Crypto.py Function check_and_join

20 views
Skip to first unread message

aditya.ka...@itbhu.ac.in

unread,
Nov 16, 2016, 12:50:23 PM11/16/16
to sympy
Hi All,
I was reading sympy crypto library where I am unable to understand something in 'check_and_join' function. 

def check_and_join(phrase, symbols=None, filter=None):
    """
    Joins characters of `phrase` and if ``symbols`` is given, raises
    an error if any character in ``phrase`` is not in ``symbols``.

    Parameters
    ==========

    phrase:     string or list of strings to be returned as a string
    symbols:    iterable of characters allowed in ``phrase``;
                if ``symbols`` is None, no checking is performed

    Examples
    ========

    >>> from sympy.crypto.crypto import check_and_join
    >>> check_and_join('a phrase')
    'a phrase'
    >>> check_and_join('a phrase'.upper().split())
    'APHRASE'
    >>> check_and_join('a phrase!'.upper().split(), 'ARE', filter=True)
    'ARAE'
    >>> check_and_join('a phrase!'.upper().split(), 'ARE')
    Traceback (most recent call last):
    ...
    ValueError: characters in phrase but not symbols: "!HPS"

    """
    rv = ''.join(''.join(phrase))
    if symbols is not None:
        symbols = check_and_join(symbols)
        missing = ''.join(list(sorted(set(rv) - set(symbols))))
        if missing:
            if not filter:
                raise ValueError(
                    'characters in phrase but not symbols: "%s"' % missing)
            rv = translate(rv, None, missing)
    return rv



This maybe very silly but use of join function twice is not clear to me. Please if anyone could help. :) 
Thanks in advance.
Aditya

Aaron Meurer

unread,
Nov 16, 2016, 2:59:54 PM11/16/16
to sy...@googlegroups.com
Seems useless to me. ''.join(s) should return s if s is a string.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/12cfea84-7132-45fd-b236-e45cbc39b956%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages