Am I doing this right ?

in #busy6 years ago (edited)

image.png

I am trying to develop a function to safely extract the Steem post_identifier from random strings (Steem transaction memos).
If the memo does not contain a post_identifier, the function should return None.

This is how far I got, before I found out, that d.tube does not use an @ before the author in their links:

def memo_to_post(memo):

    if not '@' in memo:

        return None

    reverse_memo = ''
    identifier = ''

    for char in reversed(memo):

        reverse_memo = reverse_memo + char

        if char == '@': 

            for char in reversed(reverse_memo):
    
                identifier = identifier + char

            break

    if identifier == '':

        return None

    return identifier

This is how beem tactles the issue:

def resolve_authorperm(identifier):
    """Correctly split a string containing an authorperm.
    Splits the string into author and permlink with the
    following separator: ``/``.
    """
    match = re.match("@?([\w\-\.]*)/([\w\-]*)", identifier)
    if hasattr(match, "group"):
        return match.group(1), match.group(2)
    match = re.match("([\w\-\.]+[^#?\s]+)/@?([\w\-\.]*)/([\w\-]*)", identifier)
    if not hasattr(match, "group"):
        raise ValueError("Invalid identifier")
    return match.group(2), match.group(3

With both methods, if someone was to send a d.tube link, it would not work correctly.

d.tube links look like this:

https://d.tube/#!/v/pottlund/m5cqkd1a

So, I have come up with this:

memos = [
         'bla.com/u/@user/identifier', 
         'bla.com', 
         'hello promo scam',
         '',
         'https://d.tube/#!/v/nowargraffitis/83wo23ut'
         ]

def memo_cutter(memo):

    reverse_memo = ''
    identifier = ''

    for char in reversed(memo):

        if char == '/' or char == '@': 

            break

        else:

            reverse_memo = reverse_memo + char
       
    for char in reversed(reverse_memo):

        identifier = identifier + char

    if identifier == '':

        identifier = None

    return identifier

def memo_to_identifier(memo):

    if memo == '':

        return None

    identifier = memo_cutter(memo)

    if identifier:

        author = memo_cutter(memo[:-(len(identifier) + 1 )])

    else:

        return None

    if not author:

        return None

    return (author +'/'+ identifier)
    

for memo in memos:

    identifier = memo_to_identifier(memo)

    print(str(identifier)) 

... which is kind of complicated,

but it works.

I am sure, there are much better ways to solve this question.
Probably with the split() function ...

Suggestions welcome !

Sort:  
Loading...

To the question in your title, my Magic 8-Ball says:

My reply is no

Hi! I'm a bot, and this answer was posted automatically. Check this post out for more information.

Chic article. I learned a lot of interesting and cognitive. I'm screwed up with you, I'll be glad to reciprocal subscription))

Well, the code looks like so complicated even the code itself cries.

Screen Shot 2018-07-25 at 1.40.46 PM.png

Because it is so slow!

Well, it looks good to me... I don’t know what could possibly be wrong in yiur code.. seriously!

Aaaah, so many useless replies!! 😄😄

To listen to the audio version of this article click on the play image.

Brought to you by @tts. If you find it useful please consider upvoting this reply.

Gracias @felixxx por las sugerencias para desarrollar eso que quieres. Saludos y ya hacian falta tus memes

seems like you are great developer though i am not a 1% but watching your codes is soo inspiring and motivating

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.034
BTC 66499.54
ETH 3203.31
USDT 1.00
SBD 4.14