|
Mycroft Holmes posted:
Hey since you're learning I have a few tips outside of your question as I see some bad habits forming. Writing good readable code is an easy way to get better grades. It will also make you a LOT more money. I hire python interns for my very small team with a take home test after the phone interview. About 60% will make a working project. The ones that get hired are the ones that will need the least handholding to make usable code. In no particular order: #1 For your print statements, these two things are the same but one is both easier to read and faster to write. print("Total distance traveled is:" + str(height1) + " units") -vs- print(f"Total distance traveled is: {height1} units") #2 With height1 declared up there its ambiguous that it's an initial condition or what it'll be used for. Instead try something like height_init or height_0. #3 What happens when the input to float(input("Enter the height from which the ball is dropped:")) is "george"? You'll get a value error. ANY time you're taking user input, you'll want to handle this in someway. try/except works, but so does an if statement with isinstance #4 Make comments explains tuff. #5 Gangsta Lean posted:Use a debugger or add more print statements. Learning to debug issues like this yourself is invaluable. This! Also, Python's built in logging is really easy to get started with. CarForumPoster fucked around with this message at 14:27 on Aug 16, 2021 |
# ? Aug 16, 2021 14:23 |
|
|
# ? May 15, 2024 03:41 |
Loezi posted:I don't understand what you are asking for, and I think you're falling into the XY Problem trap. Ehh, not really an XY Problem situation since I don’t actually have anything specific in mind, just curious if there was anything fun there.
|
|
# ? Aug 16, 2021 15:42 |
|
Raygereio posted:Anyone have recommendations for terminal/text UIs? I did ask about this earlier (and sadly I don't think the posted ones really solved my problems - I decided to just go with libtcod since it has some useful built-in pathfinding stuff and rather than do essentially an actual tty/ssh connection in-browser I can just squirt the game state to the browser as JSON or w/e) and after looking into them/fiddling for a bit I think Rich and Urwid seem to be the simplest/quickest to get going with - although it's worth noting I didn't do much other than read some documentation and real quickly do some experiments)
|
# ? Aug 16, 2021 16:18 |
|
CarForumPoster posted:#1 also this is really good to point out (all of them were good) , just wanted to say I pointed out the existance of f-strings to someone I work on a project with who's not natively a Python programmer and her response was "hell yeah, this is awesome"
|
# ? Aug 16, 2021 16:19 |
|
I have a friend who's a Python dev and actively avoids using f-strings, or even .format() or modulo strings. I use nothing BUT f-strings unless it's super simple or I need a raw string for some reason. Inline string processing is so much easier than concatenation.
|
# ? Aug 16, 2021 16:25 |
|
Bad Munki posted:Ehh, not really an XY Problem situation since I don’t actually have anything specific in mind, just curious if there was anything fun there. Nothing stops you from declaring a __foo__ method, no. You can make a method with any valid name. But like Loezi said, nothing would automatically use it like the other dunders are used.
|
# ? Aug 16, 2021 17:50 |
|
D34THROW posted:I have a friend who's a Python dev and actively avoids using f-strings, or even .format() or modulo strings. I use nothing BUT f-strings unless it's super simple or I need a raw string for some reason. Inline string processing is so much easier than concatenation. F-strings and str.join are life!
|
# ? Aug 16, 2021 18:00 |
|
Bad Munki posted:Ehh, not really an XY Problem situation since I don’t actually have anything specific in mind, just curious if there was anything fun there. You could make your own functions that themselves call your own custom magic functions and make them part of whatever custom classes you like. You can use them as a sort of OOP single (maybe kind-of multiple?) dispatch. You can abuse/overload existing operators and their to do strange things with the normal magic functions on your custom objects. You can do some silly things to define something approximating custom operators. They aren't special other than that they have a bunch of underscores and some of them are used by built-in functions in specific ways.
|
# ? Aug 16, 2021 18:02 |
|
Re: comments I tend to comment too much, but everybody has their own styles and I find it's easier to skip over what's there rather than scratch your head at what isn't. To that end I like writing my plan for what needs to happen in VERY broad pseudocode, almost like a sort of TODO list, and filling out each point as I start coding. Afterwards I remove the comments that are now made self-explanatory by the code, or update/clarify the ones that might have confusing code following it, to provide some context It's probably excessive but at least then you have the extra layer of "ah, here's what they WANTED to do, versus what they ACTUALLY did", and you can always tidy it up later.. but adding comments to code you didn't write and/or don't remember is pretty rough and will waste a lot of time later on
|
# ? Aug 16, 2021 20:57 |
|
D34THROW posted:I have a friend who's a Python dev and actively avoids using f-strings, or even .format() or modulo strings. I use nothing BUT f-strings unless it's super simple or I need a raw string for some reason. Inline string processing is so much easier than concatenation. What's their reasoning? Whenever I gently caress around with string concatenation it's inevitable I'll gently caress up adding a space at the end of the string or something. F-strings are perfect for just writing out a quick sentence and replacing whatever piece needs to be variable with, well, a variable. Like it's so easy to just be like print(f"The value of x was: {x}") when you're debugging or whatever.
|
# ? Aug 17, 2021 15:52 |
|
Protocol7 posted:Like it's so easy to just be like print(f"The value of x was: {x}") when you're debugging or whatever. you can make this even easier with: print(f”{x=}”) f-strings are awesome.
|
# ? Aug 17, 2021 16:00 |
|
punished milkman posted:you can make this even easier with: drat, you really do learn something new every day in this thread!
|
# ? Aug 17, 2021 16:01 |
|
Protocol7 posted:What's their reasoning? Whenever I gently caress around with string concatenation it's inevitable I'll gently caress up adding a space at the end of the string or something. F-strings are perfect for just writing out a quick sentence and replacing whatever piece needs to be variable with, well, a variable. Because shes curmudgeonly. She GETS f-strings but prefers concatenation. I guess it's easier in her head for her?
|
# ? Aug 17, 2021 19:58 |
|
punished milkman posted:you can make this even easier with: that's neat -- had to look it up for myself. it's new in 3.8 apparently
|
# ? Aug 17, 2021 23:17 |
|
Zoracle Zed posted:that's neat -- had to look it up for myself. it's new in 3.8 apparently Yea I had no idea about this!
|
# ? Aug 18, 2021 04:26 |
|
Zoracle Zed posted:that's neat -- had to look it up for myself. it's new in 3.8 apparently holy shiiiit that is sweet
|
# ? Aug 18, 2021 05:43 |
|
A lot of great stuff in recent python versions, but thanks to our institutions IT policy of "well we made linux, so naturally we need our own distribution" I'm stuck at 3.6 Or I suppose I could manually install a more recent version, but all that manual version management sounds like a great way to ensure future trouble.
|
# ? Aug 18, 2021 09:46 |
Pyenv sorts that problem right out.
|
|
# ? Aug 18, 2021 09:51 |
|
NinpoEspiritoSanto posted:Pyenv sorts that problem right out. Still more management than I should need to do.
|
# ? Aug 18, 2021 10:10 |
|
Loezi posted:"well we made linux, so naturally we need our own distribution" What exactly does this mean? Do you work for Linux?
|
# ? Aug 19, 2021 05:23 |
|
Loezi posted:Still more management than I should need to do. venvs and conda involve no more work than any linux package manager and are a hell of a lot easier than Windows software deployment, try mambaforge
|
# ? Aug 19, 2021 05:35 |
|
I have some questions about ctypes and pointers (what else would you ask about ctypes?) How would I properly work with a function argument that is char***. First, to answer "what the living gently caress?" 1. char* being one string 2. char** being an array of strings 3. char*** being a pointer to an array of strings where the function is going to stuff the allocated memory I want to turn that into a list of Python strings. A lot of these functions have output variables as arguments and return an int for error level. The second thing is that I'm not entirely sure how I'm supposed to work with a pointer to a struct as an argument. Well, PyCharm got mad about me unwrap the pointer type and rejected everything afterwards. I was going to just run the stuff tomorrow and find out how mad the code really is, but given that's the only thing in all of the bindings that it didn't like, I'm pretty sure it's going to kick my rear end.
|
# ? Aug 19, 2021 05:49 |
|
Rocko Bonaparte posted:I have some questions about ctypes and pointers (what else would you ask about ctypes?) I'm not certain but my first guess would be to write an interface function in C that accepts a char**, pass that by reference to whatever badly-written legacy code you're using, then call the interface function in Python. Use a list comprehension to create a list of Python strings from the POINTER(c_char_p)
|
# ? Aug 19, 2021 06:20 |
|
Rocko Bonaparte posted:I have some questions about ctypes and pointers (what else would you ask about ctypes?) Something like this I think. Python code:
|
# ? Aug 19, 2021 21:45 |
|
Da Mott Man posted:Something like this I think. That seemed to do it! QuarkJets posted:badly-written legacy code It was me and I wrote it a month ago! That still makes it legacy code. It's particularly goofy because it's working with a device driver through ioctls so it's having to get stuff through a hole in a sheet.
|
# ? Aug 20, 2021 03:06 |
|
I have a student who's interested in using Python to explore cybersecurity/penetration testing/etc. which is a field I don't know much about. Does anyone have any recommendations for books, sites, etc. on the topic - ideally ones aimed at the introductory level? I asked another group I'm in and got two links to dead blogs about Kali Linux, a collection of dead git repos, and some pentesting frameworks with lovely documentation. So, to clarify, I'm looking for real novice-level material - maybe something a 200-level CS student with some Linux background could digest? e: I've heard good things about Grey Hat Python, and I know that's super old - but Black Hat Python by the same author is semi-recent, would that be an okay start?
|
# ? Aug 20, 2021 03:27 |
|
It’s been a while but I remember Violent Python looking like a fun read. You can redo the TCP attack that made Mitnick famous, etc.
|
# ? Aug 20, 2021 04:14 |
|
Epsilon Plus posted:I have a student who's interested in using Python to explore cybersecurity/penetration testing/etc. which is a field I don't know much about. Does anyone have any recommendations for books, sites, etc. on the topic - ideally ones aimed at the introductory level? I asked another group I'm in and got two links to dead blogs about Kali Linux, a collection of dead git repos, and some pentesting frameworks with lovely documentation. So, to clarify, I'm looking for real novice-level material - maybe something a 200-level CS student with some Linux background could digest? David Bombal on YouTube talks about and models ethical hacking with Python which could be a good place too. There are sites like PicoCTF for ethical hacking practice although that's more cyber security in general than just Python in particular.
|
# ? Aug 20, 2021 07:27 |
|
I'd like to measure and print the memory usage (RSS) before and after each line of python code in a file, without actually calling the resource module function and print for every. single. line. I know there's decorators for functions, but are there any pointers or terms I can google to look into doing something like this?
|
# ? Aug 23, 2021 21:52 |
|
Thom ZombieForm posted:I'd like to measure and print the memory usage (RSS) before and after each line of python code in a file, without actually calling the resource module function and print for every. single. line. I know there's decorators for functions, but are there any pointers or terms I can google to look into doing something like this? https://pypi.org/project/memory-profiler/ Also recommended is https://pypi.org/project/line-profiler/. edit: If you have more specific memory needs and want to use some specific tools like heapy or something for measuring it, you can use sys.settrace() to run before each line execution. OnceIWasAnOstrich fucked around with this message at 22:14 on Aug 23, 2021 |
# ? Aug 23, 2021 22:08 |
|
I'm wanting to improve my Python some because I started a new job where I want to be relatively proficient in it, but also won't be doing Python day-in day-out, so I figured I'd make a little toy application with it like I've been doing for years in Java, Javascript, or Go. I'm wanting to do a little OAuth-driven web frontend around https://github.com/sgratzl/slack_cleaner2. I'm thinking that I want sessions to store user-specific slack API credentials that I get during the oauth flow, background processing to actually do the many, many slack API calls when it needs to crawl and delete messages of a certain type. The high level flow I'm thinking of is:
This is a toy to me, I want it to be deployed in a way where it'll work for me, but I don't need persistent storage. If I were doing this in Java, I'd use Spring Boot with in-memory sessions and async processing on a threadpool for calling Slack's API. In NodeJS, I'd freehand "sessions" in a shared data structure, have the background jobs just be promises that I don't check the result of, and just leave it a single process for my expected levels of load. I'm starting off sketching this out in Flask, but wondering if I should go for full-fat Django just to get easier sessions, although it looks like Django is going to demand a real database. From what I can tell, doing something like I'm wanting with Python is going to need some type of external in-memory storage for sessions (Redis?), I'm going to need some type of external task queue (Celery?) to manage the background processing, and I'm going to want multiple Python processes for web and multiple python process for doing the async processing. I'm probably putting this behind nginx because I know nginx extremely well and that's how all my other toys are deployed, but now I'm having to figure out uWSGI vs Gunicorn also. Am I reaching for too many tools here? How could I simplify this and do this with fewer parts and processes in Python-land? I could also just implement the worker that consumes tasks from a task queue and bang out the OAuth login flow & sessions in Java or JS in hours, but I'm doing this intentionally to improve my Python.
|
# ? Aug 25, 2021 04:39 |
|
uWSGI or Gunicorn can scale your web application for you. Other than that the Celery and Redis task queue stack is very VERY easy to implement with Flask. Your idea is basically how you would build a non-toy application in the Python land.
Da Mott Man fucked around with this message at 05:38 on Aug 25, 2021 |
# ? Aug 25, 2021 05:34 |
|
Twerk from Home posted:I'm wanting to improve my Python some because I started a new job where I want to be relatively proficient in it, but also won't be doing Python day-in day-out, so I figured I'd make a little toy application with it like I've been doing for years in Java, Javascript, or Go. If you goal is to make this simple and not worry about the queue and server finagling too much, IMO Plotly Dash is what you want. I use Plotly dash for all my simple web front ends. Usually deployed to Heroku because it's just stupid easy. Here's a complete set of instructions to deploy. Here's your dash app, I called it app.py: code:
code:
code:
code:
code:
Basically almost every question you asked becomes "doesn't matter they do it for you". This may or may not appeal to you.
|
# ? Aug 25, 2021 17:35 |
|
I appreciate the feedback from both of you! I need to handle an OAuth2 bot access tokens, so I need persistent server-side state somewhere beyond just a task queue, and I'm also hoping to host this on the same home server that I already have hosting a dozen of my previous bad ideas, so I'm going to go ahead & use uWSGI, nginx, Celery, & Redis. I've got Celery & Flask set up and working together decently, and it's fine for local development but actually deploying this means that I now have 3 new services (uWSGI, redis, celery), and I'm not at all confident in my uWSGI or celery config. I guess I'll just peek once in a while to make sure nothing's crashing. I did give up on figuring out the correct way to use the Flask application factory while still letting celery be declared globally so that I could annotate methods with @celery.task, and to my great shame I just have everything in one file right now. It looks like this would be a decent way to do it, but right now I need the app to be created before configuring celery, and need celery available globally so I'm not using the create_app() factory and just creating the app myself manually. https://blog.miguelgrinberg.com/post/celery-and-the-flask-application-factory-pattern
|
# ? Aug 25, 2021 20:52 |
|
It sounds like you've determined Django would be absolute overkill for your needs, but it's worth noting for future reference that a django-admin created barebones project is configured with a sqlite3 database, no muss no fuss.
|
# ? Aug 26, 2021 03:35 |
|
Epsilon Plus posted:It sounds like you've determined Django would be absolute overkill for your needs, but it's worth noting for future reference that a django-admin created barebones project is configured with a sqlite3 database, no muss no fuss. Actually, I am entirely regretting my decision and should have gone with Django all along. I get along with the Spring project well in Java-land, I have no idea why I didn't go with the heaviest-weight most batteries included Python framework. Can Django use that SQLite database out of the box for background task processing?
|
# ? Aug 26, 2021 19:42 |
Twerk from Home posted:Actually, I am entirely regretting my decision and should have gone with Django all along. I get along with the Spring project well in Java-land, I have no idea why I didn't go with the heaviest-weight most batteries included Python framework. Sure, it's just another database. The ORM abstracts away nearly all distinctions. (Yes there are some caveats and DB-specific hacks you can do, but they're not really relevant here)
|
|
# ? Aug 26, 2021 19:58 |
|
Data Graham posted:Sure, it's just another database. The ORM abstracts away nearly all distinctions. Thanks. I'm guessing something like this would be the solution that one would reach for on Django: https://django-background-tasks.readthedocs.io/en/latest/ I still seem to need another service to manage the background processing python process, vs being able to use the same pool of Python processes to serve web requests and poll for background jobs. I'm probably not going to change because I did get this working on Flask / Celery / Redis, but it's good to know.
|
# ? Aug 26, 2021 20:01 |
Hmm. Maybe, but that's a 2018 vintage so I'd be leery. I'd say it's still Celery as the go-to for Django (or pubsub/kafka for big deployments using GCS or AWS). No reason you can't use the same approach in Django as you did in Flask. Data Graham fucked around with this message at 21:04 on Aug 26, 2021 |
|
# ? Aug 26, 2021 21:01 |
|
|
# ? May 15, 2024 03:41 |
|
I'll second the Celery recommendation in case you need that.
|
# ? Aug 26, 2021 22:10 |