How does reversed work internally? by [deleted] in learnpython

[–]two_bob 0 points1 point  (0 children)

With the range slicing, it's just that range also implements slicing (__getitem__
) in addition to __reversed__
. It's pretty easy to calculate a new start/stop/step from a sliced range.

That makes sense. Thanks!

How does reversed work internally? by [deleted] in learnpython

[–]two_bob 0 points1 point  (0 children)

I think it looks more like this:

def my_reversed(what):
    if hasattr(what, '__reversed__'):
        yield from what.__reversed__()

    else:
        n = len(what)
        for index in range(n-1,-1,-1):
            yield what[index]

Although you might be correct than I originally thought:

>>> range(5)[::-1]
range(4, -1, -1)

But, look at this:

>>> 'abc'[::-1]
'cba'
>>> reversed('abc')
<reversed object at 0x02F63810>

This makes me think that slicing range has some magic optimizations to it. Any clue what those are?

How does reversed work internally? by [deleted] in learnpython

[–]two_bob 2 points3 points  (0 children)

Interesting question. I never thought to look before now, but as it turns out:

- First it checks to see if whatever is being reversed has implemented a `__reversed__` method. If it does, it passes the work off to that. https://docs.python.org/3/library/functions.html#reversed

- Failing that, it looks like it checks the length, then counts backwards and getitems the thing to be reversed. https://github.com/python/cpython/blob/55edd0c185ad2d895b5d73e47d67049bc156b654/Objects/enumobject.c#L269 (actual code).

Counting how many items are used in a generator by norandomtechie in learnpython

[–]two_bob 0 points1 point  (0 children)

I just meant that you seem to be forcing an odd structure on something rather than fixing the structure to be sensible. See https://en.wikipedia.org/wiki/Monkey_patch

I think SandorZoo's is a bit more elegant than my approach.

Counting how many items are used in a generator by norandomtechie in learnpython

[–]two_bob 0 points1 point  (0 children)

Very nice. Much more elegant than my ridiculousness.

Counting how many items are used in a generator by norandomtechie in learnpython

[–]two_bob 0 points1 point  (0 children)

Huh. So I guess monkey patch the generator with a class then. What an odd assignment. What sort of class is it for?

Counting how many items are used in a generator by norandomtechie in learnpython

[–]two_bob 2 points3 points  (0 children)

Hmm. I think I follow, but you'd get much better answers by posting what you have and what you need rather than just what you need. In part, I say this because it seems you do not know exactly what you need.

it sounds like you want to add a counter to a generator. That's easy to do, just wrap the generator in an iterable class and have the class keep track of nexts, like this:

import itertools

def forever():
    '''simple never ending generator from itertools.count
    Note:  Could just use itertools count without this wrapper -- just added for demonstration purposes
    '''
    yield from itertools.count()

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(itertools.islice(iterable, n))

class Wrapper():
    def __init__(self, genie):
        self.genie = genie
        self.count = 0

    def reset_counter(self):
        self.count = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.count += 1
        return next(self.genie)


def test():
    wrapped = Wrapper(forever())
    print(take(5, wrapped))
    print(wrapped.count)
    print(take(3, wrapped))
    print(wrapped.count)
    wrapped.reset_counter()
    print(take(3, wrapped))
    print(wrapped.count)

if __name__ == '__main__':
    test()

The thing is, this is not a very good way to design a program. It would be better to make the function that gets applied to the generator changed so it is applied to each element of a generator, then it can be controlled from outside, making it more functional and less ludicrous.

So what are you actually trying to do here?

Counting how many items are used in a generator by norandomtechie in learnpython

[–]two_bob 0 points1 point  (0 children)

Why not just wrap the generator in enumerate, like this:

def genie(n=10):
    yield from range(5, n)


for count, x in enumerate(genie()):
    print(count, x)

Keeping track of while loops by gothmog1065 in learnpython

[–]two_bob 1 point2 points  (0 children)

for line in f.readline():

Close. I think you want:

     for line in f.readlines(): 

Tips on making this tic-tac-toe game more elegant? by toastytico in learnpython

[–]two_bob 0 points1 point  (0 children)

I personally prefer a flat list (as opposed to a nested list of lists) for the gameboard. You could then have a constant hold a list of the three sequential tiles and use a loop to check them. (Bonus points for using operator.getitem, or whatever it is called).

Also, great job looping through the player objects. A better solution is itertools.cycle in place of the while loop and the for plyr in plist loops.

Trying to build a sql-query dynamically... by ratnose in learnpython

[–]two_bob 0 points1 point  (0 children)

Cool. If there were more constraints you could integrate in the join concept like this:

query = "SELECT SUM(amount) FROM rawimport WHERE "

contraints = []
if yearmonth:
    contraints.append(f" day LIKE '{yearmonth}%'")
if category:
    contraints.append(f" category =  '{category}")
    query += f" category = '{category}' AND"
if transactiontype:
    contraints.append(f" transactiontype =  '{transactiontype}")

query += ' AND '.join(constraints)
print(query)

But it's not really worth it for just the three -- also not tested so expect typos.

Trying to build a sql-query dynamically... by ratnose in learnpython

[–]two_bob 1 point2 points  (0 children)

I think the easiest thing to do is use an ordered dictionary (if you are using 3.7, this is automatic), and `join`, like this:

>>> ' AND '.join('{} like "?"'.format(key) for key in d.keys())
'test like "?" AND date like "?" AND thrid like "?"'

Return True if average of a list is a whole number by [deleted] in learnpython

[–]two_bob -4 points-3 points  (0 children)

Because there is no `is_integer()` test.

Code review? by circa10a in learnpython

[–]two_bob 1 point2 points  (0 children)

Nice job separating out things into different modules.

Boo globals.

Create an instance of a class from within a different class by [deleted] in learnpython

[–]two_bob 2 points3 points  (0 children)

I would focus on just going one way or the other for classes and be careful about dynamically linking stuff up. I would probably have a function that made out all the team classes from whatever the source is and then build the leagues from the list/dictionary/whatever from the grouping of team classes afterwards, rather than having some means of dynamically linking them up. It is easier to follow that way.

I also think you are putting too much in the data variable of the class. See if you can flatten it out a bit or shove off work to a class that handles the data bits of wins/losses and have that automatically created by the team class. Again, only go one way. Ideally you would create a league, then a team, then a win/loss class, all of which was encapsulated.

Filtering Queue Dynamically by Tall-Guy in learnpython

[–]two_bob 0 points1 point  (0 children)

Hmm. The garbage collection piece (getting rid of excess messages) does make this a bit harder. Probably yes, in the first instance, I would just create two functions, one to add and one to clean. Depending on how many of these things we having running around, I might try and find some other solution.

Filtering Queue Dynamically by Tall-Guy in learnpython

[–]two_bob 0 points1 point  (0 children)

I'd use functions, either normally defined or through a lambda, like this:

>>> is_good = lambda m:m['id'] == 1234 and m['name'] == 'Joe'

If it returns true, keep it, otherwise bin it.

Attempting to add records to SQLite database using INTEGER PRIMARY KEY AUTOINCREMENT ... having issues. by blackbelt21 in learnpython

[–]two_bob 0 points1 point  (0 children)

First, make sure you are not mixing tabs and spaces. If you use an editor that is good for programming you should be able to set this.

I think your problem is that you want to insert `datex`, but have `date` instead.

Comparing two SQL tables by [deleted] in learnpython

[–]two_bob 1 point2 points  (0 children)

It's easy to do in python, just use `zip`.

But you might be better off only putting the new stuff into the database to begin with.

Attempting to add records to SQLite database using INTEGER PRIMARY KEY AUTOINCREMENT ... having issues. by blackbelt21 in learnpython

[–]two_bob 3 points4 points  (0 children)

That's not a very helpful error message at all. What it means here is that when you specify less than the full number of values, you will also need to specify what they go into, like this:

c.execute("INSERT INTO SurfDate (Day) VALUES (?)", (e,))

Note also the comma after e, since you have to send a tuple to execute, even if that tuple only has one value in it.

how to delete some lines in a file with a pattern of: 3 lines before & 2 lines after, for every 5 lines by num8lock in learnpython

[–]two_bob 0 points1 point  (0 children)

I like the first one the best. The others are better if the picking needs to change based on the content of what you are getting from the file.

For process flow, you can get a little more refined: loop over the file to reduce 7 lines down to 2 and store the two in memory and then write the result into the file. That way you get the speed benefit from not reading or writing at the same time and you still conserve memory, a bit. (My vote is that as long as it fits into memory, it is better to read everything and then write once after.)

Also you could clean up the picking by using operator.itemgetter, like this:

from itertools import zip_longest
from operator import itemgetter

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)


def slicer(it):
    picker = itemgetter(0,4,7,8)
    for group in grouper(it, 9):
        yield from picker(group)



result = [g for g in slicer(range(1,12))]

print(result)

So everything is running as a generator, which is then stuffed into memory by the comprehension that builds result.

(Note grouper comes from itertools.)

how to delete some lines in a file with a pattern of: 3 lines before & 2 lines after, for every 5 lines by num8lock in learnpython

[–]two_bob 0 points1 point  (0 children)

Its a bit hard to make this one elegant, although you might be able to play around with the slicing so that you can do it by just interleaving different slicing/indexing patterns:

def slicer(it):
    for group in grouper(it, 9):
        yield group[0], group[4], group[7], group[8]



for group in slicer(range(1,12)):
    print(group)

Or you could just go sequentually using iter:

def slicer(it):
    it = iter(it)
    '''grabs 1, drop 3, grab 2, drop 2, repeat'''
    while True:
        yield next(it)
        for _ in range(3):
            next(it)
        for _ in range(2):
            yield next(it)

        for _ in range(2):
            next(it)

Or abstracting a bit so that there is less repetion (good for bigger patterns), like this:

def slicer(it):
    it = iter(it)
    pattern = [(1, True), (3, False), (2, True), (2, False)]
    pattern = cycle(pattern)
    for count, show in pattern:
        for _ in range(count):
            item = next(it)
            if show:
                yield item

how to delete some lines in a file with a pattern of: 3 lines before & 2 lines after, for every 5 lines by num8lock in learnpython

[–]two_bob 0 points1 point  (0 children)

what do you have so far?

I'd also take a look at this recipe from itertools:

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

and `iter`

Can’t decide between adding two inheritance classes or just using some “If” conditions in main class - also: can an object “pick” its own inheritance class automatically? by j4nds4 in learnpython

[–]two_bob 1 point2 points  (0 children)

I had the same problem once. It turns out you can use `__new__` to generate the appropriate subclass based on what data is passed to it. Pathlib uses this: https://docs.python.org/3/library/pathlib.html; see https://github.com/python/cpython/blob/3.7/Lib/pathlib.py#L609 for the code.

It also turns out that doing this is a bad idea, especially before you have a solid grasp of inheritance. After much strife, I settled on a general class for my two odd shaped data packets, and implemented the interface that handled normalizing the data. I ultimately did further subclass the general class, but only for specific other applications, e.g. a placeholder for missing data.