Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
CarForumPoster
Jun 26, 2013

⚡POWER⚡

Mycroft Holmes posted:

code:
# variables
bounces1 = 1
height1 = float(0)

# input
height = float(input("Enter the height from which the ball is dropped:"))
index = float(input("Enter the bounciness index of the ball:"))
bounces = float(input("Enter the number of times the ball is allowed to continue bouncing:"))

# calcs
height1 = height + (height*index)
bounce = height * index
if index > 0:
    while bounces1 < (bounces+1):
        bounce = bounce * index
        height1 = height1 + bounce
        bounces1 = bounces1 + 1
    print("Total distance traveled is:" + str(height1) + " units")
else:
    print("Total distance traveled is:" + str(height1) + " units")


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

Adbot
ADBOT LOVES YOU

Bad Munki
Nov 4, 2008

We're all mad here.


Loezi posted:

I don't understand what you are asking for, and I think you're falling into the XY Problem trap.

Dunders are, fundementally, for interacting with python buildins: you would implement a __len__ to describe what len(butts) means, or a __call__ to describe what butts() means. Or you could do things with __slots__ and __setattr__ to fiddle with what butts.farts = 1 does.

What are you trying to achieve with __shapely__, given that there would be no python builtin that knew to call it?

Is the point that you would like to look at an object and ask "do you have a __shapely__"? What are you, fundamentally, trying to achieve?

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.

death cob for cutie
Dec 30, 2006

dwarves won't delve no more
too much splatting down on Zot:4

Raygereio posted:

Anyone have recommendations for terminal/text UIs?
I've made a script to automate measurements for a multiplexer and want to make it quicker to use (so a user doesn't have to edit the script when they want to change between measuring voltage or resistance, or tweak measurement intervals).
I could make an interface with tkinter, pysimplegui, QT, or whatever, but that feels like it would be to elaborate for the actual functionality it needs to have. I also could bash my head against curses until I get something nice, but that would go against my desire to not spend too much time on it.

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)

death cob for cutie
Dec 30, 2006

dwarves won't delve no more
too much splatting down on Zot:4

CarForumPoster posted:

#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")

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"

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
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.

MonkeyMaker
May 22, 2006

What's your poison, sir?

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.

Hollow Talk
Feb 2, 2014

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!

OnceIWasAnOstrich
Jul 22, 2006

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.

boofhead
Feb 18, 2021

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

Macichne Leainig
Jul 26, 2012

by VG

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.

punished milkman
Dec 5, 2018

would have won

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.

Macichne Leainig
Jul 26, 2012

by VG

punished milkman posted:

you can make this even easier with:

print(f”{x=}”)

f-strings are awesome.

drat, you really do learn something new every day in this thread!

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

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.

Like it's so easy to just be like print(f"The value of x was: {x}") when you're debugging or whatever.

Because shes curmudgeonly. She GETS f-strings but prefers concatenation. I guess it's easier in her head for her?

Zoracle Zed
Jul 10, 2001

punished milkman posted:

you can make this even easier with:

print(f”{x=}”)

f-strings are awesome.

that's neat -- had to look it up for myself. it's new in 3.8 apparently

CarForumPoster
Jun 26, 2013

⚡POWER⚡

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!

QuarkJets
Sep 8, 2008

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

Loezi
Dec 18, 2012

Never buy the cheap stuff
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.

NinpoEspiritoSanto
Oct 22, 2013




Pyenv sorts that problem right out.

Loezi
Dec 18, 2012

Never buy the cheap stuff

NinpoEspiritoSanto posted:

Pyenv sorts that problem right out.

Still more management than I should need to do.

qsvui
Aug 23, 2003
some crazy thing

Loezi posted:

"well we made linux, so naturally we need our own distribution"

What exactly does this mean? Do you work for Linux?

QuarkJets
Sep 8, 2008

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

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
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.

QuarkJets
Sep 8, 2008

Rocko Bonaparte posted:

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.

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)

Da Mott Man
Aug 3, 2012


Rocko Bonaparte posted:

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.

Something like this I think.

Python code:
lib = CDLL("somedll.dll")
data = POINTER(c_char_p)()
lib.some_func(byref(data))

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Da Mott Man posted:

Something like this I think.

Python code:
lib = CDLL("somedll.dll")
data = POINTER(c_char_p)()
lib.some_func(byref(data))

That seemed to do it!


QuarkJets posted:

badly-written legacy code

It was me and I wrote it a month ago! :sweatdrop:

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.

death cob for cutie
Dec 30, 2006

dwarves won't delve no more
too much splatting down on Zot:4
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?

Hed
Mar 31, 2004

Fun Shoe
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.

Sad Panda
Sep 22, 2004

I'm a Sad Panda.

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?

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?

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.

Thom ZombieForm
Oct 29, 2010

I will eat you alive
I will eat you alive
I will eat you alive
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?

OnceIWasAnOstrich
Jul 22, 2006

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

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.
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:
  • Link to slack via OAuth to allow the app access it needs and learn who the user is
  • User inputs how they want to run the cleaner
  • slack_cleaner runs in the background until it's done cleaning.

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.

Da Mott Man
Aug 3, 2012


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

CarForumPoster
Jun 26, 2013

⚡POWER⚡

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.

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:
  • Link to slack via OAuth to allow the app access it needs and learn who the user is
  • User inputs how they want to run the cleaner
  • slack_cleaner runs in the background until it's done cleaning.

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.

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:
import dash
import dash_bootstrap_components as dbc

app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = dbc.Container(
    dbc.Alert("Hello Bootstrap!", color="success"),
    className="p-5",
)

if __name__ == "__main__":
    app.run_server()
You make a file called Procfile
code:
web: gunicorn app:server
Have your requirements.txt:
code:
dash
dash-bootstrap-components
gunicorn
To set up for deployment to staging:
code:
heroku login
heroku git:remote -a your-heroku-app-here
To update:
code:
git push heroku master
Plotly Dash 2.0 was announced yesterday, a dev version is out now, and one of the things they added was easier support for long (think >10 sec) callbacks. You'll use redis which can be set up on Heroku pretty easily and IIRC they obfuscate away the celery bit.

Basically almost every question you asked becomes "doesn't matter they do it for you". This may or may not appeal to you.

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.
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

death cob for cutie
Dec 30, 2006

dwarves won't delve no more
too much splatting down on Zot:4
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.

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.

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?

Data Graham
Dec 28, 2009

📈📊🍪😋



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.

Can Django use that SQLite database out of the box for background task processing?

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)

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.

Data Graham posted:

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)

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.

Data Graham
Dec 28, 2009

📈📊🍪😋



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

Adbot
ADBOT LOVES YOU

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
I'll second the Celery recommendation in case you need that.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply