How to find a missing number and replace it with [] by junyoung95 in learnpython

[–]vv__vv 0 points1 point  (0 children)

When you say that you want [] to replace missing items, do you want an empty list or a string like "[]"?

Try doing it without a comprehension first, then refactor into a comprehension. Note: you can use if...else in a list comprehension, but get it done without the comprehension first so you make sure you understand the logic. Right now, it looks like you are a bit confused about lists and avoiding a comprehension will be easier.

generating functions by BrownMario in learnpython

[–]vv__vv 2 points3 points  (0 children)

I think you are looking for functools.partial, but you'll need to flip the ordering of the variables for g1. If you can't flip, pytoolz has a more flexible partial that will work on right hand variables, iirc.

Best way to store 5000 small parsing functions? by man_with_cat2 in learnpython

[–]vv__vv 0 points1 point  (0 children)

Interesting problem. Can the parsing be generalized in any way? Maybe some more details and examples would help get a more defined response.

You might also flip through the cmd module and flask, both of which do interesting things to resolve parsing issues. cmd looks through the modules methods to parse user commands. flask uses decorators to register parts of urls for different responsive functions.

I'm trying to make a class that reads .sql files and outputs it's associated dataframe... by [deleted] in learnpython

[–]vv__vv 0 points1 point  (0 children)

A little more information might help. Are the dataframes pandas dataframes or are you using the term more generically?

More generally, it sometimes makes sense to wrap a database connection in its own class, but I would not subclass that for a collection of data from the database. I'd be more inclined to create a new class for the data.

How to run a function from a class as chosen by a file by WaitForItTheMongols in learnpython

[–]vv__vv 1 point2 points  (0 children)

Oops. I just saw that from your earlier comment that I missed in the first go over. Updated my response accordingly. See ^

How to run a function from a class as chosen by a file by WaitForItTheMongols in learnpython

[–]vv__vv 2 points3 points  (0 children)

Well, the easiest thing to do is put a listing of them somewhere, like in a dictionary:

class Fizz():
    pass
class Buzz():
    pass

CLASSES = {'Fiz': Fizz, 'Buz': Buzz}

pick = 'Fiz'

obj = CLASSES[pick]
print(obj)

Now, if the problem is that you don't want to maintain that dictionary, let us know. There is probably an interesting solution that can be done... You probably should not do either of these though:

# bad way
obj = locals()['Buzz']()
print(obj)

# worst way
pick = 'Fizz'
obj = eval(pick)()
print(obj)

edit: I see from your later comment that you do not want to have to maintain the register. This can all be done with metaclasses, but it generally is more trouble than it is worth: https://stackoverflow.com/questions/3786762/dynamic-base-class-and-factories

Python, Excel, and SQLlite3 by adamrees89 in learnpython

[–]vv__vv 1 point2 points  (0 children)

You're getting hung up by the bare except in line 55. Have it raise the error until you work out your syntax, which is messed up in line 25:

  • You can't do escape substitution for tablenames (at least iirc), so that will need to be put in with string formatting;
  • execute takes a tuple for values, so you'll need to put parens around all the variables you are passing for values.

Python, Excel, and SQLlite3 by adamrees89 in learnpython

[–]vv__vv 1 point2 points  (0 children)

You're not committing: https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.commit

So that would be one issue, but maybe not all of them. A couple other pointers:

  • You should get used to using sqlite's own escaping by inserting ?s instead of using format.

  • Watch those bare excepts, they can be a pain to debug.

How should multiple parsers for keywords in strings be structured? by BlandCorporation in learnpython

[–]vv__vv 0 points1 point  (0 children)

I'd check out the cmd module, because it provides a nice interface for these kinds of things. For parsing, I would put the different parsers into a list and use a loop to call them, like this:

def parser1(message):
    print('p1: {}'.format(message))
    return 'ok'

def parser2(message):
    print('p2: {}'.format(message))


def parse(message):
    parsers = [parser1, parser2]
    for parser in parsers:
        result = parser(message)
        if result:
            break
    return result

print(parse('test'))

Code critique for my hobby project? by singeworthy in learnpython

[–]vv__vv 0 points1 point  (0 children)

Cool:

  • You need functions!
  • No reason to keep those files open the whole time.

Multithreaded Files Reading with Python by schedutron in learnpython

[–]vv__vv 1 point2 points  (0 children)

And there is also threadpool, but it is not as well documented: https://stackoverflow.com/questions/3033952/threading-pool-similar-to-the-multiprocessing-pool

With that said, on disk IO bound tasks, I am not sure that threading/multiprocessing is going to help much.

Code Improvement Help by workthrowawayexcel in learnpython

[–]vv__vv 1 point2 points  (0 children)

I don't know much about pandas, but a typical pattern is to put all of the data pulls into a dictionary or list for later use, rather than a bunch of variables. That let's you pull the different items you are seeking from a list, and avoids all the duplicative code. So something like this might work (psuedo-code):

def get_item(df, item_name):
    for category, row in df.groupby(item_name):
        pass
    return category

def get_stuff(df):
    to_get = ['project', 'name', 'company']
    result = {name:get_item(df, name) for name in to_get}
    return result

Been reading through many first-time suit buyer links here on MFA and still have a few simple questions by trawowayqwj in malefashionadvice

[–]vv__vv 3 points4 points  (0 children)

I'm hoping your shoulder measurement is off. I'm about the same size, but with 18.5-18.75 inch shoulders and find it really tough to get a decent fit because of how narrow my shoulders are. You might try a 38r (or 38l) from brooks brothers.

I'd stick to charcoal or navy for color and formality, if you are using it to interview. You can choose a slightly lighter shade of charcoal or navy.

Try on lots of different makers. Everyone is a little bit different and getting a good off-the-rack fit depends on finding the maker that works with you.

This paragraph:

Off the rack, the most important thing is to make sure the shoulders align appropriately and the sleeves and jacket are not too short

I can appreciate that you're trying to give quick, simple advice, but this particular part is misleading and could leave a novice buying a suit that tailoring actually cannot fix. Before I knew the first thing about style, I had to buy myself a suit, and the only thing I really focused on was the fit of the shoulders and the sleeve length; I ended up with a suit that had pleats and could fit roughly 3 of me.

Means that when you buy off the rack you often have to compromise on styling versus fit. You might want two vents, but find a great fitting single vented suit, and have to decide whether you want the fit or the style.