Python string.printable() Examples

The following are 30 code examples for showing how to use string.printable(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module string , or try the search function .

Example 1
Project: BASS   Author: Cisco-Talos   File: avclass_common.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_sample_info(vt_rep, from_vt):
        '''Parse and extract sample information from JSON line
           Returns a SampleInfo named tuple: md5, sha1, sha256, label_pairs 
        '''
        label_pairs = []
        if from_vt:
            try:
                scans = vt_rep['scans']
            except KeyError:
                return None
            for av, res in scans.items():
                if res['detected']:
                    label = res['result']
                    clean_label = filter(lambda x: x in string.printable,
                                         label).strip().encode('utf-8').strip()
                    label_pairs.append((av, clean_label))
        else:
            label_pairs = vt_rep['av_labels']

        return SampleInfo(vt_rep['md5'], vt_rep['sha1'], vt_rep['sha256'],
                          label_pairs) 
Example 2
Project: OpenTrader   Author: OpenTrading   File: tabview.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _search_validator(self, ch):
        """Fix Enter and backspace for textbox.

        Used as an aux function for the textpad.edit method

        """
        if ch == curses.ascii.NL:  # Enter
            return curses.ascii.BEL
        elif ch == 127:  # Backspace
            self.search_str = self.textpad.gather().strip().lower()[:-1]
            return 8
        else:
            if 0 < ch < 256:
                c = chr(ch)
                if c in string.printable:
                    res = self.textpad.gather().strip().lower()
                    self.search_str = res + chr(ch)
                    self.search_results(look_in_cur=True)
                    self.display()
            return ch 
Example 3
Project: asn1tools   Author: eerimoq   File: constraints_checker.py    License: MIT License 6 votes vote down vote up
def encode(self, data):
        length = len(data)

        if not self.is_in_range(length):
            raise ConstraintsError(
                'Expected between {} and {} characters, but got {}.'.format(
                    self.minimum,
                    self.maximum,
                    length))

        if self.permitted_alphabet is None:
            return

        for character in data:
            if character not in self.permitted_alphabet:
                raise ConstraintsError(
                    "Expected a character in '{}', but got '{}' (0x{:02x}).".format(
                        ''.join([c if c in string.printable[:-5] else '.'
                                 for c in self.permitted_alphabet]),
                        character if character in string.printable else '.',
                        ord(character))) 
Example 4
Project: rekall   Author: google   File: parsers.py    License: GNU General Public License v2.0 6 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
    other_chars = unicode(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example 5
Project: rekall   Author: google   File: yara_support.py    License: GNU General Public License v2.0 6 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(list(map(ord, opener_and_closer)))
    other_chars = str(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example 6
Project: Karta   Author: CheckPointSW   File: strings.py    License: MIT License 6 votes vote down vote up
def nextGlobalString(self, ea):
        """Find the next possible address for a global string, given the beginning of the current global string.

        Args:
            ea (int): effective start address of the current global string.

        Return Value:
            Possible start address for the next global string
        """
        str_content = self.getAsciiString(ea)
        if str_content is None:
            return ea + self._global_alignment
        elif idc.get_wide_byte(ea + len(str_content)) != ord('\0'):
            return ea + max(self._global_alignment, pad(len(str_content), self._global_alignment))
        else:
            for offset in range(len(str_content) - 1, -1, -1):
                if chr(str_content[offset]) not in string.printable:
                    return ea + max(self._global_alignment, pad(offset, self._global_alignment))
        return ea + self._global_alignment 
Example 7
Project: CapTipper   Author: omriher   File: pescanner.py    License: GNU General Public License v3.0 6 votes vote down vote up
def check_yara(self, data):
        ret = []
        if self.rules:
            yarahits = self.rules.match(data=data)
            if yarahits:
              for hit in yarahits:
                ret.append("YARA: %s" % hit.rule)
                #for key, val in hit.strings.iteritems():
                for (key,stringname,val) in hit.strings:
                    makehex = False
                    for char in val:
                        if char not in string.printable:
                            makehex = True
                            break
                    if makehex == True:
                        ret.append("   %s => %s" % (hex(key), binascii.hexlify(val)))
                    else:
                        ret.append("   %s => %s" % (hex(key), val))
        return '\n'.join(ret) 
Example 8
Project: CapTipper   Author: omriher   File: __init__.py    License: GNU General Public License v3.0 6 votes vote down vote up
def handle_string(self, token_text):
        if self.last_type in ['TK_START_BLOCK', 'TK_END_BLOCK', 'TK_SEMICOLON']:
            self.append_newline()
        elif self.last_type == 'TK_WORD':
            self.append(' ')

        # Try to replace readable \x-encoded characters with their equivalent,
        # if it is possible (e.g. '\x41\x42\x43\x01' becomes 'ABC\x01').
        def unescape(match):
            block, code = match.group(0, 1)
            char = chr(int(code, 16))
            if block.count('\\') == 1 and char in string.printable:
                return char
            return block

        token_text = re.sub(r'\\{1,2}x([a-fA-F0-9]{2})', unescape, token_text)

        self.append(token_text) 
Example 9
Project: tmtoolkit   Author: WZBSocialScienceCenter   File: corpus.py    License: Apache License 2.0 6 votes vote down vote up
def filter_characters(self, allow_chars=string.printable, drop_chars=None):
        """
        Filter the document strings by removing all characters but those in `allow_chars` or, if `allow_chars` evaluates
        to False, remove those in `drop_chars`.

        :param allow_chars: set (like ``{'a', 'b', 'c'}`` or string sequence (like ``'abc'``)
        :param drop_chars: set or string sequence of characters to remove (if `allow_chars` evaluates to False)
        :return: this instance
        """

        if allow_chars is not None:
            if not isinstance(allow_chars, set):
                allow_chars = set(allow_chars)

            drop_chars = ''.join(self.unique_characters - allow_chars)
        else:
            if isinstance(drop_chars, (set, list, tuple)):
                drop_chars = ''.join(drop_chars)

            if not isinstance(drop_chars, str):
                raise ValueError('`drop_chars` must be a sequence, set or string if `allow_chars` is not given')

        return self.replace_characters(str.maketrans(drop_chars, drop_chars, drop_chars)) 
Example 10
Project: clusterfuzz   Author: google   File: libfuzzer.py    License: Apache License 2.0 6 votes vote down vote up
def _restart_qemu(self):
    """Restart QEMU."""
    logs.log_warn(
        'Connection to fuzzing VM lost. Restarting.',
        processes=process_handler.get_runtime_snapshot())
    stop_qemu()

    # Do this after the stop, to make sure everything is flushed
    if os.path.exists(QemuProcess.LOG_PATH):
      with open(QemuProcess.LOG_PATH) as f:
        # Strip non-printable characters at beginning of qemu log
        qemu_log = ''.join(c for c in f.read() if c in string.printable)
        logs.log_warn(qemu_log)
    else:
      logs.log_error('Qemu log not found in {}'.format(QemuProcess.LOG_PATH))

    start_qemu()
    self._setup_device_and_fuzzer() 
Example 11
Project: glusto-tests   Author: gluster   File: test_enabling_brick_mux.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_enabling_brick_mux_with_wrong_values(self):
        """
        Test Case:
        - Create a gluster cluster
        - Set cluster.brick-multiplex value to random string(Must fail)
        - Set cluster.brick-multiplex value to random int(Must fail)
        - Set cluster.brick-multiplex value to random
          special characters(Must fail)
        """
        # Creation of random data for cluster.brick-multiplex option
        # Data has: alphabets, numbers, punctuations and their combinations
        key = 'cluster.brick-multiplex'
        for char_type in (string.ascii_letters, string.punctuation,
                          string.printable, string.digits):

            temp_val = self.get_random_string(char_type)
            value = "{}".format(temp_val)
            ret = set_volume_options(self.mnode, 'all', {key: value})
            self.assertFalse(ret, "Unexpected: Erroneous value {}, to option "
                             "{} should result in failure".format(value, key))
            g.log.info("Expected: Erroneous value %s, to option "
                       "%s resulted in failure", value, key) 
Example 12
Project: privacyidea   Author: privacyidea   File: log.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def format(self, record):
        try:
            message = super(SecureFormatter, self).format(record)
        except TypeError:
            # In pyhton 2.6 the Formatter does not seem to 
            # be defined as 
            # class Formatter(object)
            # Using it in the super-statement this will raise a TypeError
            message = Formatter.format(self, record)
        secured = False

        s = ""
        for c in message:
            if c in string.printable:
                s += c
            else:
                s += '.'
                secured = True

        if secured:
            s = "!!!Log Entry Secured by SecureFormatter!!! " + s

        return s 
Example 13
Project: streamlink   Author: streamlink   File: argparser.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def convert_arg_line_to_args(self, line):
        # Strip any non-printable characters that might be in the
        # beginning of the line (e.g. Unicode BOM marker).
        match = _printable_re.search(line)
        if not match:
            return
        line = line[match.start():].strip()

        # Skip lines that do not start with a valid option (e.g. comments)
        option = _option_re.match(line)
        if not option:
            return

        name, value = option.group("name", "value")
        if name and value:
            yield u"--{0}={1}".format(name, value)
        elif name:
            yield u"--{0}".format(name) 
Example 14
Project: qubes-core-admin   Author: QubesOS   File: backup.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def _monitor_process(proc, error_message):
        try:
            yield from proc.wait()
        except:
            proc.terminate()
            raise

        if proc.returncode:
            if proc.stderr is not None:
                proc_stderr = (yield from proc.stderr.read())
                proc_stderr = proc_stderr.decode('ascii', errors='ignore')
                proc_stderr = ''.join(
                    c for c in proc_stderr if c in string.printable and
                                              c not in '\r\n%{}')
                error_message += ': ' + proc_stderr
            raise qubes.exc.QubesException(error_message) 
Example 15
Project: universe   Author: openai   File: vnc_action_space.py    License: MIT License 6 votes vote down vote up
def __init__(self, keys=None, buttonmasks=None, screen_shape=(1024, 728)):
        self.keys = []
        if keys is None:
            keys = [c for c in string.printable] + list(constants.KEYMAP.keys())
        for key in (keys or []):
            down = vnc_event.KeyEvent.by_name(key, down=True)
            up = vnc_event.KeyEvent.by_name(key, down=False)
            self.keys.append(down)
            self.keys.append(up)
        self._key_set = set(self.keys)

        self.screen_shape = screen_shape
        if self.screen_shape is not None:
            self.buttonmasks = []
            if buttonmasks is None:
                buttonmasks = range(256)
            for buttonmask in buttonmasks:
                self.buttonmasks.append(buttonmask)
            self._buttonmask_set = set(self.buttonmasks) 
Example 16
Project: multibootusb   Author: mbusb   File: gen.py    License: GNU General Public License v2.0 5 votes vote down vote up
def strings(filename, _min=4):
    with open(filename, errors="ignore") as f:
        result = ""
        for c in f.read():
            if c in string.printable:
                result += c
                continue
            if len(result) >= _min:
                yield result
            result = ""
        if len(result) >= _min:  # catch result at EOF
            yield result 
Example 17
Project: CAMISIM   Author: CAMI-challenge   File: validator.py    License: Apache License 2.0 5 votes vote down vote up
def validate_characters(self, text, legal_alphabet=string.printable, key=None, silent=False):
		"""
			Validate that only legal characters are contained in a text

			@attention:

			@param text: Some string
			@type text: str | unicode
			@param legal_alphabet: String of legal characters
			@type legal_alphabet: str | unicode
			@param key: If True, no error message will be made
			@type key: basestring | None
			@param silent: If True, no error message will be made
			@type silent: bool

			@return: bool
			@rtype: bool
		"""
		prefix = ""
		if key:
			prefix = "'{}' ".format(key)

		set_legal_alphabet = set(legal_alphabet)
		set_text = set(text)
		if not set_legal_alphabet.issuperset(set_text):
			if not silent:
				difference = set_text.difference(set_legal_alphabet)
				difference.discard(set_legal_alphabet)
				self._logger.error("{}Invalid characters: '{}'".format(prefix, ", ".join(difference)))
			return False
		return True 
Example 18
Project: DOTA_models   Author: ringringyi   File: gen_data.py    License: Apache License 2.0 5 votes vote down vote up
def make_vocab_ids(vocab_filename):
  if FLAGS.output_char:
    ret = dict([(char, i) for i, char in enumerate(string.printable)])
    ret[data.EOS_TOKEN] = len(string.printable)
    return ret
  else:
    with open(vocab_filename) as vocab_f:
      return dict([(line.strip(), i) for i, line in enumerate(vocab_f)]) 
Example 19
Project: DOTA_models   Author: ringringyi   File: model_test.py    License: Apache License 2.0 5 votes vote down vote up
def create_fake_charset(num_char_classes):
  charset = {}
  for i in xrange(num_char_classes):
    charset[i] = string.printable[i % len(string.printable)]
  return charset 
Example 20
Project: suffix-trees   Author: ptrus   File: test_gen.py    License: MIT License 5 votes vote down vote up
def string_and_substring(draw):
    x = draw(text(min_size=2, alphabet=string.printable))
    i = draw(integers(min_value=0, max_value=len(x)-2))
    j = draw(integers(min_value=i+1, max_value=len(x)-1))
    return (x, x[i:j]) 
Example 21
Project: suffix-trees   Author: ptrus   File: test_gen.py    License: MIT License 5 votes vote down vote up
def string_and_not_substring(draw):
    x = draw(text(min_size=2, alphabet=string.printable))
    i = draw(integers(min_value=1, max_value=len(x)-1))
    y = ''.join(sample(x, i))
    assume(x.find(y) == -1)
    return (x, y) 
Example 22
Project: pwnypack   Author: edibledinos   File: codec.py    License: MIT License 5 votes vote down vote up
def frequency_app(parser, cmd, args):  # pragma: no cover
    """
    perform frequency analysis on a value.
    """

    parser.add_argument('value', help='the value to analyse, read from stdin if omitted', nargs='?')
    args = parser.parse_args(args)
    data = frequency(six.iterbytes(pwnypack.main.binary_value_or_stdin(args.value)))
    return '\n'.join(
        '0x%02x (%c): %d' % (key, chr(key), value)
        if key >= 32 and chr(key) in string.printable else
        '0x%02x ---: %d' % (key, value)
        for key, value in data.items()
    ) 
Example 23
Project: asn1tools   Author: eerimoq   File: per.py    License: MIT License 5 votes vote down vote up
def encode(self, value):
        try:
            return self.encode_map[value]
        except KeyError:
            raise EncodeError(
                "Expected a character in '{}', but got '{}' (0x{:02x})'.".format(
                    ''.join(sorted([chr(v) for v in self.encode_map])),
                    chr(value) if chr(value) in string.printable else '.',
                    value)) 
Example 24
Project: sato   Author: megagonlabs   File: bag_of_characters.py    License: Apache License 2.0 5 votes vote down vote up
def extract_bag_of_characters_features(data, n_val):
    
    characters_to_check = [ '['+  c + ']' for c in string.printable if c not in ( '\n', '\\', '\v', '\r', '\t', '^' )] + ['[\\\\]', '[\^]']
    
    f = OrderedDict()

    f['n_values'] = n_val
    data_no_null = data.dropna()
    all_value_features = OrderedDict()

    all_value_features['length'] = data_no_null.apply(len)

    for c in characters_to_check:
        all_value_features['n_{}'.format(c)] = data_no_null.str.count(c)
        
    for value_feature_name, value_features in all_value_features.items():
        f['{}-agg-any'.format(value_feature_name)] = any(value_features)
        f['{}-agg-all'.format(value_feature_name)] = all(value_features)
        f['{}-agg-mean'.format(value_feature_name)] = np.mean(value_features)
        f['{}-agg-var'.format(value_feature_name)] = np.var(value_features)
        f['{}-agg-min'.format(value_feature_name)] = np.min(value_features)
        f['{}-agg-max'.format(value_feature_name)] = np.max(value_features)
        f['{}-agg-median'.format(value_feature_name)] = np.median(value_features)
        f['{}-agg-sum'.format(value_feature_name)] = np.sum(value_features)
        f['{}-agg-kurtosis'.format(value_feature_name)] = kurtosis(value_features)
        f['{}-agg-skewness'.format(value_feature_name)] = skew(value_features)

    n_none = data.size - data_no_null.size - len([ e for e in data if e == ''])
    f['none-agg-has'] = n_none > 0
    f['none-agg-percent'] = n_none / len(data)
    f['none-agg-num'] = n_none
    f['none-agg-all'] = (n_none == len(data))
    #print(len(f))
    return f 
Example 25
Project: rpg-text   Author: Dogeek   File: map-creator.py    License: MIT License 5 votes vote down vote up
def get_avg_charwidth(widget=None, text=None):
    if text is None:
        text = string.printable
    if widget is None:
        font = tkfont.Font(font='TkTextFont')
    else:
        font = tkfont.Font(font=widget['font'])
    return sum([font.measure(c) for c in text]) / len(text) 
Example 26
Project: arctic   Author: man-group   File: test_compress_integration.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def random_string(N):
    _str = ''.join(random.choice(list(string.printable) + ['hello', 'world', 'hellworld', 'Hello', 'w0rld']) for _ in six.moves.xrange(int(N)))
    return _str.encode('ascii') 
Example 27
Project: arctic   Author: man-group   File: test_compress.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def random_string(N):
    _str = ''.join(random.choice(string.printable) for _ in range(int(N)))
    return _str.encode('ascii') 
Example 28
Project: moler   Author: nokia   File: helpers.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def non_printable_chars_to_hex(source):
    """
    Converts input string into hex for all non printable chars, printable chars remain unchanged.
    :param source: input string.
    :return: output string witch exchanged chars.
    """
    import string
    output = ""
    for char in source:
        if char not in string.printable or char in ['\n', '\r']:
            output += "\\x{:02x}".format(ord(char))
        else:
            output += char
    return output 
Example 29
Project: linguistic-style-transfer   Author: vineetjohn   File: sentiment_140_adapter.py    License: Apache License 2.0 5 votes vote down vote up
def clean_word(word):
    word = "" if (len(word) > 3 and word[:4] == "http") else word
    word = "" if (any([char not in string.printable for char in word])) else word
    return word 
Example 30
Project: GelReportModels   Author: genomicsengland   File: avrotools.py    License: Apache License 2.0 5 votes vote down vote up
def handleString(self, length=10, characters=string.printable):
        return ''.join(
            [random.choice(characters) for _ in range(length)])