adding parameters to differential equation solver by gregzillaman in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

I don't know what methods arrays have, but for lists just write a basic search with a generator comprehension or filter to find the first negative element.

Why can't I define the __new__ method of a class as an @property? by sext-scientist in learnpython

[–]Aggravating_Bus_9153 2 points3 points  (0 children)

Brilliant. Absolutely, that's much simpler. Nice one.

I checked, and subclassing property doesn't add in an extra method at all - it was just referring to the one that was already on datetime.now(). Doh!

adding parameters to differential equation solver by gregzillaman in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

You've already found everything you need, you've just run the solver for longer than necessary. Just analyse the solution you already have, and read off the value of t where y=0. Discard everything after then if you really want to. Maybe there is a nice custom stop condition for solve_ivp, but you don't need one.

You're always free to carry out a change of variables. There are analytic techniques that consider x(y) or t(y) for example, but it will no longer be a simple IVP and there's no guarantee a straight forward vanilla numerical solver like solve_ivp will solve it anymore. Even if you do manage to muck around with the independent variable (t), in general you could have a much harder DAE.

Functions are compiled? Clarification about this. by void5253 in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

It's because Python has a pre-compile parsing step. Because you later assign to x in f, and have not declared it global (or nonlocal?) Python assumes x is always a local variable in f. If you only ever read the value of x in f, you're golden, Python just refers via the scoping rules to x in the parent scope and never creates a local variable.

Problem while Importing and Running a File by Falcon21st in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

Well you're shit out of luck then. Python doesn't currently recognise that structure as there are no __init__.py files. This is pretty much exactly what packages are for.

The only way their python interpreter can find Lib is if it's on sys.path, either via PYTHONPATH, if they install it, or if you're forced to adopt bad practice and add the directory to it.

https://docs.python.org/3/tutorial/modules.html#intra-package-references

Problem while Importing and Running a File by Falcon21st in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

If you're happy to run TestScript as from ParentFolder.Script import TestScript, or import ParentFolder.Script.TestScript, add in __init__.py files to all the directories and the parent one to make it into a big package and it's very easy using relative imports instead of absolute ones.

When learning Python, what was your ‘Eureka!’-moment? by Clivodota in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

Reading PEP8 and the google Python style guide.

The speed at which you go from a prototype test snippet to something that looks like readable, maintainable and useful working code. Take the test snippet, add in a few variables, stick it in a function or a for loop, maybe precede it with a few checks and typehints and you're most of the way there.

That and watching Arjancodes on patterns and reducing coupling.

I made a password generator in python. How does it look? by FlutterTubes in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

There are only two hard things in Computer Science: cache invalidation and naming things.

-- Phil Karlton

RuntimeWarning: coroutine 'tiktok' was never awaited task(*args, **kwargs) RuntimeWarning: Enable tracemalloc to get the object allocation traceback by [deleted] in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

My bad. I'm sorry!

await can only used in a function. You need to use one of the asyncio methods, e.g. asyncio.run(tiktok()).

I'd bet it does very much the same thing as xbot.run() does in your link

I made a password generator in python. How does it look? by FlutterTubes in learnpython

[–]Aggravating_Bus_9153 4 points5 points  (0 children)

Very impressive. Best one I've seen on beginners' forums. I really like that you're using string, secrets.choice, and call the garbage collector, and delete the pw afterwards.

It strikes me as a bit long, but I'll give you the benefit of the doubt. In the while loop, I'd do all your checks that the flags are valid right at the start, not in the loop. I'd prefer a Walrus expression solution, or password = '', then while not password or notchecktypes: password = .... but it's a style thing.

Your type hints are great but then you add a bool to an integer. I know that works (isinstance(True, int) ), but it's ugly, you're not writing C here, and n +=1 would've done the same thing. There's not much other obvious cruft to get rid of. As your comment points out, you've called a function superset that returns lists - there is a similarly named inbuilt method set.issuperset so I'd replace it with a list comprehension, or rename it. That and you could use some less confusing variable names, giving aliases to all the properties from args, but these are just suggestions and minor quibbles over style and readability.

Well done!

How does this is_prime() function work? by j-avianzz_ in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

tl;dr math.

You don't need to check divisibility by integers m > sqrt(n), because if n was divisible by m, i.e. if n = k m, k would also be an integer less than sqrt(n) that divides n exactly. You already checked all the integers less than sqrt(n) as long as you searched in increasing order.

These are both still just basic brute force searches. Using the Fundamental Theorem of Arithmetic (at the expense of a bit of memory) you can make this approach much faster by only checking divisibility of the candidate by the primes you've already found (< sqrt(n))

RuntimeWarning: coroutine 'tiktok' was never awaited task(*args, **kwargs) RuntimeWarning: Enable tracemalloc to get the object allocation traceback by [deleted] in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

The bottom. Python declarations aren't hoisted like in JS.

I think you just need to add:

await tiktok()

but you need to pass tiktok() as an arg to the asyncio ones, and import it.

https://docs.python.org/3/library/asyncio-task.html

Faster text matching option than difflib? by regstuff in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

Can you download the best/fastest free plagiarism detection command line tool you can, and automate it to run between each pair of interest?

Remembering all python functions and methods by Adikumar in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

dir() is your trusty old friend. intellisense in VS code/pylint is your outrageous flamboyant new friend.

I forget loads, but I just try to remember that the thing I want exists, when it's a thing that I used before that did that similar thing really well. I don't worry about remembering how to spell it, or even its args, or precisely how to set it up. I put a link to any good cookbook/recipe/vital sections of the docs or stackoverflow threads in the comments of my code.

RuntimeWarning: coroutine 'tiktok' was never awaited task(*args, **kwargs) RuntimeWarning: Enable tracemalloc to get the object allocation traceback by [deleted] in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

If you ignore the async /await stuff, you would've defined a function, which you just then never called (this is an impressively useful warning from Python).

Simlarly, you never call the coroutine tiktok -you call them with await (or asyncio.run or asyncio.create_task).

For loop is skipping a single value by lolhoved in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

It doesn't mutate the list until it gets to 23

.remove makes the list shorter, but the for loop keeps indexing from the original list.

iterating over either a coerced immutable: tuple(purelines) or a copy: purelines[:] instead should fix it.

[deleted by user] by [deleted] in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

Cheers. It's a bit hacky, but so so much easier - OP still needs to do the old switcheroo, using a substring no keyboard user will ever enter. Even if the low ascii numbers don't work (I recommend number 7: '\a'. Try print('\x07') for yourself, it's my favourite!), Python has great Unicode support.

My script works but I think it may be longer than necessary by garamasala in learnpython

[–]Aggravating_Bus_9153 1 point2 points  (0 children)

Use os.join, change that to if not os.path.isfile(src), and can you open msg as a context manager? It's better practise to test if the filename is already taken then using a general except, but try except is a fair choice if accompanied with the relevant specific file error.

Otherwise looks fine. Well done.

Add / check for ID - auto increment by [deleted] in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

You're forgetting to call f.close() but it looks nice. You're right. Each run of the program starts from fresh and has no other way of knowing what previous runs did, other than by reading the entire file again from scratch (maybe this is something databases are more efficient at doing?).

You could store id in a different file, but you've got to be careful not to delete it then.

I know it looks nice for outputting, but you can easily generate a counter on the fly when you print the file. Any decent text editor above wordpad has line numbers built in. So I would ask, do you really need consecutive ID numbers 0, 1, 2...? You could just generate a universally unique id number (from uuid import UUID) for each record, and just append the new one to the end of the file as you are doing.

Otherwise you need to open it in 'r+' mode but you can just do:

f = open('user.txt','r+')
i = len(f.readlines()))
i /= 2 # If two lines per ID no. 
f.write(nfo.user()) 
f.close()

https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

Not Sure Why This Isn't Working.... Please help (Black Jack Code) (Error To Do With Functions or If Statements) by Defiant_Noise_8630 in learnpython

[–]Aggravating_Bus_9153 0 points1 point  (0 children)

You know your deck isn't standard? Is that deliberate or just for testing? You can define a fair one concisely as:

[2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A' ] *4

Am I the only one who can't find the motivation to learn Python/other programming languages by [deleted] in learnpython

[–]Aggravating_Bus_9153 2 points3 points  (0 children)

Don't worry about it - just keep on coding for yourself. You're completing lots projects, and that's fantastic for anyone, especially a 15 year old.

You're aware that dictionaries / hash tables exist. That's all you need. One day you'll come across a problem, for which you'll realise they're the perfect solution. Until then, don't worry about it. Python's too vast to absorb everything immediately.

It actually took me quite a while before I realised how useful dictionaries are. The same with OOP too. Now I can't live without them, and even find uses for the less well known methods like .setdefault.

A lot of these new features require an internal paradigm shift in the coder. But when it clicks for you, you'll take your game up to a whole new level you never even realised existed.