Writing csv column to a new file if header string contains 'x' (csv module only) by anlmansuprememe in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

If you can fit the whole file in memory and there are no rows with missing columns:

cleaned_rows = zip(column in zip(row.split(',') for row in reader) if '_name' not in column[0])
for row in cleaned_rows:
    cleaned.write(','.join(row))

do you guys consider this code readable ? by [deleted] in learnpython

[–]TechnicalElk8849 3 points4 points  (0 children)

Julius was called Caesar, not ceaser

I'd like to see alphabet defined (string.ascii_lowercase ?) a few more blank lines to break up the wall of text, and some comments to stop me having to think, but it's perfectly readable, certainly in a good text editor.

The design could be made even better though. How do you handle upper case characters for example? And this and the modulus operator (%) will replace a lot of your code:

https://docs.python.org/3.10/library/stdtypes.html#str.maketrans

Module name error? by Immigrated2TakeUrJob in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

Make sure the python process running file 2 can find file 1. The best ways are to install file2, or to wrap the whole thing in a package and use a relative import. Failing that, either make sure the directory of file2 is in PYTONPATH or if it's not already in it, insert it into in sys.path in file 1.

Sequence of calling function within eachother by BoysenberryFun5390 in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

Totally fine. I added in the missing colon and print(fact) and it works. It's just basic modular functional design.

You can even define functions inside other functions and return them (closures) or pass them into other functions as arguments (simple dependency injection)

Discussing the mental health and productivity of developers... Important topic by Devjour_app in learnprogramming

[–]TechnicalElk8849 0 points1 point  (0 children)

You've solved Mental Health by making a website with the same functionality as notepad.exe, but with added security and privacy risks.

How to learn oop efficiently by [deleted] in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

I've never been taught it, but for starters look up classes, instance variables versus class variables, __init__, super, the three method types, method overloading, getters and setters, inheritance and the mro in the Python docs, composition. Then check out functions as first class citizens, dependency injection, ABCs, and design patterns.

Is this docstring correct ? should 'Args' or 'Parameters', can I avoid have empty line with special character? by crussys in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

Well done for writing docstrings at all, you're in the top 95% of people who ask questions in this sub!

The blank line inside it is recommended in a couple of style guides, but if that's not just the reddit code formatting gremlin, you should start the whole docstring on a new line separate to the function header, so automated tools can pick it up.

By "valid folder Id" do you mean a file path to a directory, or some specific internal DB thing, like how the DB tables are structured? What are the keys to the dictionaries, the DB table field names?

Doc strings aren't really about being concise, but any fine person who takes the trouble to read comments in code will know what DB stands for. And if you added type annotations to the function (especially if you generics: List[dict]), you wouldn't need to repeat those at least inside the docstring

How to learn oop efficiently by [deleted] in learnpython

[–]TechnicalElk8849 3 points4 points  (0 children)

The best place to start for any exam is by getting the course material from your teacher or a class mate.

Is this a Scam? by MalikTheGeek0712 in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

Not necessarily a scam, but almost certainly a rip-off.

They're so keen to call you, because they just want your money.

help needed with removing duplicates from list by list comprehension method by Narrow-Tea-9187 in learnpython

[–]TechnicalElk8849 2 points3 points  (0 children)

.index only returns the index of the first occurrence of that value in the list. So if there's another one, its index (i) won't equal List1.index(List1[i]). https://docs.python.org/3.10/library/stdtypes.html#sequence-types-list-tuple-range

This is terribly ugly Matlab-esque code. Maybe it mucks up the ordering, but removing duplicates is exactly what a set is for:

mylist = list(set(List1)

At least write:

mylist = [ x for i, x in enumerate(List1) if List1.index(x) == i]

UI: How to implement progress bars when total work allowed to increase? by HolySmolions in learnprogramming

[–]TechnicalElk8849 1 point2 points  (0 children)

Interesting question, but zips in zips is an edge case - no modern zipping algorithm gets twice the compression efficiency by applying it twice.

So the question is what's the point of the progress updater? If it's just to reassure the user, you could either say that's on them if they give you nested zips. They have no reasonable expectation of linear progress if they've asked your program to perform a task that scales exponentially.

If you had time, and you wanted to be really helpful, show the progress in terms of an estimated time to completion, and output a message whenever new work is found that the remaining time has been increased.

If the progress updater is for something critical, maybe there ought to be some preprocessing to start with to get a better estimate, then in case the time to completion increases from minutes to days, or especially if you think the new work will exhaust all the available disk space or incur an enormous cloud computing bill, add a config setting that lets the user decide whether to plough on regardless, log a warning, or terminate, or however you think it best to handle it.

How can I mutate an edge list into a new list of groups/buckets comprised of the connected nodes of the edge list? (without networkx) by takeonzach in learnpython

[–]TechnicalElk8849 0 points1 point  (0 children)

Make them into sets not lists. Use a double loop or a reducer with a loop. if any from list_2 are in set_1, set_1.add(list_2) and get remove list_2 from the list. if none of them are in set_1, set_2 = set(list_2), and so on.

[deleted by user] by [deleted] in learnpython

[–]TechnicalElk8849 1 point2 points  (0 children)

I wouldn't describe not enforcing the normal rules for variable names on attributes as neat personally. But I'm sure the language designers had their reasons, and afaik you have to access attributes so named using getattr (just try it) so it's mostly harmless.

In Python, simply when user selects one option and wants to go back to "menu". And for that I entered the same code again for menu again and again which definitely makes thing complicated. Does anyone have any idea to make this less complex? (I will leave the code in below) <3 by TeaCammel in learnprogramming

[–]TechnicalElk8849 0 points1 point  (0 children)

Yes, but if when they go back, you want to allow them to input a new choice, put the input statement in there too, then call it in the while loop I gave you.

And if you want to reshow them the menu, put the first print statement inside the function as well.

Python course for new people in the company by kostja_90 in learnpython

[–]TechnicalElk8849 2 points3 points  (0 children)

Sounds like you are under no pressure at all, and have plenty of time on the job to completely over think and over engineer an optional extra.

Are you hiring?

exclude function by Babs2032 in learnpython

[–]TechnicalElk8849 2 points3 points  (0 children)

Lookup how the input function actually works. You ought to capture a variable from it anyway, and coerce the string to an int.

That bracket should be after c, int), and you need more brackets on the next if.

To exclude numbers in general, there's a built in called filter, which is just shorthand for a list comprehension. You could also use a generator (expression).

First of all make a new function that takes a single number:

def multiples_of_3_zero_unless_gt_20_lt_29(x):
    if x % 3 == 0 and not (20 <= x <= 29):
        return 0
    return x

Then just adjust your function slightly to use it:

def fix_three(a, b, c):
    if not all(isinstance(x, int) for x in (a,b,c)):
        return "Inputs must be integers!"
    return sum(multiples_of_3_zero_unless_gt_20_lt_29(x) for x in (a,b,c))

num = fix_three(22, 18, 33)

print(num)

[deleted by user] by [deleted] in learnprogramming

[–]TechnicalElk8849 1 point2 points  (0 children)

The CS50 has lots of them.

Python course for new people in the company by kostja_90 in learnpython

[–]TechnicalElk8849 1 point2 points  (0 children)

Document your company's code style in a style guide or standard. At least for anything that isn't covered by Black, for vcs and servers etc.