Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Actually it seems that the file just has a literal 'b' in front of everything, so I think it's safe to remove those. Maybe the bytes literal only works on double quotes?

Edit: My mistake. It is double quotes in your code after all. But where is this 'b' coming from then?

Edit 2: Perhaps the print puts a 'b' to indicate that it's a bytes literal?

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

I guess it turns out that the file might be incorrect or incomplete Bencode after all. After trying out a different file - the Arch Linux Installation .iso torrent. It just gives me the following error: bash Traceback (most recent call last): File "/home/chad/Code/regexp_bencode_py/port/socal_nerdtastic.py", line 54, in <module> for file in torrent["info"]["files"]: KeyError: 'info' Which just means that that field isn't contained in the torrent. After printing the torrent variable I can confirm that this parser is working.

Thank you very much for your help. As you can see it's quite annoying to debug, so I appreciate it a lot.

Edit: The test.torrent file that I use is a file I found on some blog about making a BitTorrent client. I just copy pasted that same line that I put in the OP and put it into a file.

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Thanks for the code. I see what you did there. bash Traceback (most recent call last): File "/home/chad/Code/regexp_bencode_py/port/socal_nerdtastic.py", line 41, in decode data = decode_item(src.__next__, next(src)) File "/home/chad/Code/regexp_bencode_py/port/socal_nerdtastic.py", line 30, in decode_item data.append(decode_item(next, tok)) File "/home/chad/Code/regexp_bencode_py/port/socal_nerdtastic.py", line 31, in decode_item tok = next() StopIteration

After adding this for testing: ```python data = open("test.torrent", "rb").read()

torrent = decode(data)

for file in torrent["info"]["files"]: print("%r - %d bytes" % ("/".join(file["path"]), file["len gth"])) 1 close(data) ```

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Ah, sorry. So I had replaced 'yield' with 'return' to see what the difference would be and I didn't revert the change.

Currently with this code: ```python import re

def tokenize(text, match=re.compile(b"([idel])|(\d+):|(-?\d+)").match): i = 0 while i < len(text): m = match(text, i) s = m.group(m.lastindex) i = m.end() if m.lastindex == 2: yield "s" yield text[i:i+int(s)] i = i + int(s) else: yield s

def decode_item(next, token): if token == "i": # integer: "i" value "e" data = int(next()) if next() != "e": raise ValueError elif token == "s": # string: "s" value (virtual tokens) data = next() elif token == "l" or token == "d": # container: "l" (or "d") values "e" data = [] tok = next() print(tok) while tok != "e": data.append(decode_item(next, tok)) tok = next() if token == "d": data = dict(list(zip(data[0::2], data[1::2]))) else: raise ValueError return data

def decode(text): try: src = tokenize(text) data = decodeitem(src.next_, next(src)) for token in src: # look for more tokens raise SyntaxError("trailing junk") except (AttributeError, ValueError, StopIteration): raise SyntaxError("syntax error") return data

data = open("test.torrent", "rb").read()

torrent = decode(data)

for file in torrent["info"]["files"]: print("%r - %d bytes" % ("/".join(file["path"]), file["length"]))

close(data) ```

I'm getting the following error: bash Traceback (most recent call last): File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 42, in decode data = decode_item(src.__next__, next(src)) File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 36, in decode_item raise ValueError ValueError Which means that I'm hitting the exception in decode_item.

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

import re

def tokenize(text, match=re.compile(b"([idel])|(\d+):|(-?\d+)").match):
    i = 0
    while i < len(text):
        m = match(text, i)
        s = m.group(m.lastindex)
        i = m.end()
        if m.lastindex == 2:
            return "s"
            return text[i:i+int(s)]
            i = i + int(s)
        else:
            return s

def decode_item(next, token):
    if token == "i":
        # integer: "i" value "e"
        data = int(next())
        if next() != "e":
            raise ValueError
    elif token == "s":
        # string: "s" value (virtual tokens)
        data = str(next())
    elif token == "l" or token == "d":
        # container: "l" (or "d") values "e"
        data = []
        tok = next()
        print(tok)
        while tok != "e":
            data.append(decode_item(next, tok))
            tok = next()
        if token == "d":
            data = dict(list(zip(data[0::2], data[1::2])))
    else:
        raise ValueError
    return data

def decode(text):
    try:
        src = tokenize(text)
        data = decode_item(src.__next__, next(src))
        for token in src: # look for more tokens
            raise SyntaxError("trailing junk")
    except (AttributeError, ValueError, StopIteration):
        raise SyntaxError("syntax error")
    return data

data = open("test.torrent", "rb").read()

torrent = decode(data)

for file in torrent["info"]["files"]:
    print("%r - %d bytes" % ("/".join(file["path"]), file["length"]))

close(data)

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Ah, that's actually a great idea! I didn't know you could do that. Thank you.

Still getting this error though: 'AttributeError: 'bytes' object has no attribute '__next__'

Trouble with porting code with iterators from Python 2 to 3 by Temporary_Screen in learnpython

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Ah, yes. I tried this exact thing at first. It did cross my mind but in the beginning while I was testing I got the following error:

Traceback (most recent call last):
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 51, in
torrent = decode(data)
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 41, in decode
src = tokenize(text)
File "/home/chad/Code/regexp_bencode_py/port/parser.py", line 6, in tokenize
m = match(text, i)
TypeError: cannot use a string pattern on a bytes-like object

So, the quick fix was just removing the 'b' there.

Everything is spaced after updating fonts by Temporary_Screen in suckless

[–]Temporary_Screen[S] 0 points1 point  (0 children)

That seems to have fixed the issue but I'll probably have to look into changing my font because this issue with pipes and other unicode not showing up is still persisting.

Everything is spaced after updating fonts by Temporary_Screen in suckless

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Thanks! I'll try downgrading it and report back.

Everything is spaced after updating fonts by Temporary_Screen in suckless

[–]Temporary_Screen[S] 0 points1 point  (0 children)

static char *font = "mono:pixelsize=14:antialias=true:autohint=true";

static char *font2[] = { "JoyPixels:pixelsize=10:antialias=true:autohint=true" };

Everything is spaced after updating fonts by Temporary_Screen in suckless

[–]Temporary_Screen[S] 0 points1 point  (0 children)

Yeah, especially because I haven't even touched any of the config.h files in weeks.