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
FAT32 SHAMER
Aug 16, 2012



Basically his port 80 and 443 were blocked by Comcast and those are the only ports the webhooks would work with so we spent a bunch of time, energy, (and he spent money) for naught which feels super bad (only because I wasted his time by not even thinking about this in the planning stages and telling him his little sign that lights up on a webhook won't work)

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

funny Star Wars parody posted:

Basically his port 80 and 443 were blocked by Comcast and those are the only ports the webhooks would work with so we spent a bunch of time, energy, (and he spent money) for naught which feels super bad (only because I wasted his time by not even thinking about this in the planning stages and telling him his little sign that lights up on a webhook won't work)

Ah bummer. I suppose you could use AWS or Heroku free tier to host something listening on :80 or :443 and then whatever message could get passed along to the raspberry pi by a number of other methods.

FAT32 SHAMER
Aug 16, 2012



fletcher posted:

Ah bummer. I suppose you could use AWS or Heroku free tier to host something listening on :80 or :443 and then whatever message could get passed along to the raspberry pi by a number of other methods.

Yeah probably, but that would be significantly more work than my pay grade (free) haha

Methanar
Sep 26, 2013

by the sex ghost

funny Star Wars parody posted:

Yeah probably, but that would be significantly more work than my pay grade (free) haha

Actually this bit would be super easy to do.

apt-get install haproxy

then drop in something like this

code:

listen http-frontend
  bind 0.0.0.0:443
  mode http
  default_backend pi-backend

backend pi-backend
  mode http
  server RaspberryPi 10.10.10.10:81
#Comcast IP 

Loezi
Dec 18, 2012

Never buy the cheap stuff
I'm doing something relatively simple and Bottle seems like it's fully adequate for my uses. Is there some reason for using Flask over it if I don't need authentication or anything like that? In practice, I only need to serve an API with three distinct endpoints, one of which takes query parameters.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Loezi posted:

I'm doing something relatively simple and Bottle seems like it's fully adequate for my uses. Is there some reason for using Flask over it if I don't need authentication or anything like that? In practice, I only need to serve an API with three distinct endpoints, one of which takes query parameters.

Bottle is fine.

reversefungi
Nov 27, 2003

Master of the high hat!

funny Star Wars parody posted:

Yeah probably, but that would be significantly more work than my pay grade (free) haha

Maybe ngrok might be helpful in this situation?

https://ngrok.com/

accipter
Sep 12, 2003
What's the preferred program to create an Windows executable from a Python script these days?

My Rhythmic Crotch
Jan 13, 2011

I am looking for a python module to help me do some design/sim type of work. I want to build an object as a 3d array of points, and be able to interact with the object (zoom, rotate, etc). A 3D surface plot in matplotlib is almost exactly what I want, except when viewing the plot it's really clunky and hard to interact with. So basically something just like that, except less frustrating. There doesn't seem to be much out there for this. Any suggestions welcomed.

Cingulate
Oct 23, 2012

by Fluffdaddy

My Rhythmic Crotch posted:

I am looking for a python module to help me do some design/sim type of work. I want to build an object as a 3d array of points, and be able to interact with the object (zoom, rotate, etc). A 3D surface plot in matplotlib is almost exactly what I want, except when viewing the plot it's really clunky and hard to interact with. So basically something just like that, except less frustrating. There doesn't seem to be much out there for this. Any suggestions welcomed.
We use Mayavi to visualize brains in 3D and rotate them. However, it's a terrible piece of software, if at all possible, you should use something else.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
I've never heard of Mayavi before but you might want to also check out Paraview. Both of them are built using the VTK library so should be quite similar. I'm pretty sure that everybody I know (including me) that would use a visualization software would use Paraview though.

This might help:

https://pyscience.wordpress.com/2014/09/06/numpy-to-vtk-converting-your-numpy-arrays-to-vtk-arrays-and-files/

a few DRUNK BONERS
Mar 25, 2016

Is there a Python library for doing testing when my tests operate on large chunks of text, too large to just put in the code? Obviously I can just put the input and output into files and then read from them, but I know this is going to be bad when I have several hundred tests and dependencies on filenames. I think I'm going to need some kind of testing database- what's the least painful way to do this?

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

a few DRUNK BONERS posted:

Is there a Python library for doing testing when my tests operate on large chunks of text, too large to just put in the code? Obviously I can just put the input and output into files and then read from them, but I know this is going to be bad when I have several hundred tests and dependencies on filenames. I think I'm going to need some kind of testing database- what's the least painful way to do this?

Dependencies on file names sounds like you've got them hard-coded somewhere, which is really bad practice. You can use magic mocks to over-write those file names for testing, but you're probably better off restructuring your code to make it more testable.

huhu
Feb 24, 2006
I would like to debug JavaScript inside a PyCharm for Flask projects. I see that I can set breakpoints in the JS code but PyCharm always skips over them. The only tutorial I can find online involves debugging static HTML and not Jinja2 templates.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

huhu posted:

I would like to debug JavaScript inside a PyCharm for Flask projects. I see that I can set breakpoints in the JS code but PyCharm always skips over them. The only tutorial I can find online involves debugging static HTML and not Jinja2 templates.

I haven't debugged JS in a jetbrains IDE in years, but I seem to recall it was easier if I used an external js file included in my templates via a script tag.

huhu
Feb 24, 2006

Thermopyle posted:

I haven't debugged JS in a jetbrains IDE in years, but I seem to recall it was easier if I used an external js file included in my templates via a script tag.

I might be explaining this poorly. Here's my structure:

app/templates/input.html:
code:
<button>Do some JS from input.js</button>
...
<script src="{{url_for('baseViews.appstatic', filename='scripts/input.js')}}"></script>
...
app/static/scripts/input.js:
code:
// Code here for <button>
When I set a breakpoint in input.js for code related to <button> and then click it, the breakpoints are ignored.

Edit: The settings I think are relevent:

huhu fucked around with this message at 16:26 on May 30, 2017

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell


You'll probably get more help on StackOverflow. I will say this...the reason I haven't used the JetBrains JS debugger in years is that the chrome devtools debugger is superior in every way that I can think of. Depending on what you're doing you might be better off just using that.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
For those that work with Excel, what Python library do you use?

Dex
May 26, 2006

Quintuple x!!!

Would not escrow again.

VERY MISLEADING!

Hughmoris posted:

For those that work with Excel, what Python library do you use?

i don't do anything complicated with excel but openpyxl suits me fine

Dominoes
Sep 20, 2007

XLSXwriter can do pretty-much everything, but it's missing some useful abstractions. Ie the code will be kind of brute force, iterating over row indexes etc. The API and docs's nice. I don't know how it compares to OpenPyxl; they're both popular, full-featured libraries; after researching which to go with, I ended up neutral, and picked writer arbitrarily. Those are the only two full-featured, modern libraries.

Dominoes fucked around with this message at 11:58 on Jun 1, 2017

accipter
Sep 12, 2003

Hughmoris posted:

For those that work with Excel, what Python library do you use?

I typically use openpyxl, but if there are charts as sheets then I might have to use xlwings. Note that xlwings opens up an Excel instance so it can be slow. If you are using tabular data, there is a function within pandas to also read excel. If you are mixing xls and xlsx, you might want to use pyexcel that has support for both.

LochNessMonster
Feb 3, 2005

I need about three fitty


So I've been messing around with Python for a few months but I feel I'm stuck at a level slightly above complete beginner. I know how to work with lists/dicts/tuples and create loops/conditions.

What would be a good resource to gain knowledge on working with concepts that are new to me? Things I regularly see and don't understand are __init__ or __main__ funtions, when I should use classes or how to create a decent structure in my code. Currently I create a few functions and below them I write loops/conditions which use them. When looking at code from others I usually see them split over multiple files.

I'm working with python3.

Dominoes
Sep 20, 2007

__init__ is boilerplate used in classes. Its most typical use is to convert arguments that are supplied when creating instances of the class into class-scope 'self' variables. Ie:


Python code:
class Snake
    def __init__(self, age, personality,  adult):
        self.age = age
        self.personality = personality
        self.adult = adult
You could also add other things to pre-calculate. This example also includes how you'd access these 'self' variables you're creating.
Python code:
class Snake
    def __init__(self, age, personality,  adult):
        self.age = age
        self.personality = personality
        self.adult = adult

        self.length = age * 5 if adult else age * 10

    # Another double-underscore function demonstrating how the arguments you give the new class 
    # instance are now available by self. - prefixed methods; see what it's for below.
    def __string__(self): 
        return f'{A {self.personality} snake {self.age} years old'
When you create an instance like this, it runs the code in your __init__ method.
Python code:
martha = Snake(5, 'friendly', True)

print(martha)  # A friendly snake 5 years old
__main__ is boilerplate used when executing your file directly. ie:

Python code:
if __name__ == "__main__":
    run_all()
Code structure is a complex, nuanced subject that depends on your project and personal preference. Dive into a project and experiment, then ask specific questions about it when it comes up. Same for when to use classes. Some people use them everywhere as abstract chunks of code. I prefer them for relatively concrete things that have many instances, things like the snake example above, or for creating new data types.

Dominoes fucked around with this message at 12:18 on Jun 1, 2017

Eela6
May 25, 2007
Shredded Hen

LochNessMonster posted:

So I've been messing around with Python for a few months but I feel I'm stuck at a level slightly above complete beginner. I know how to work with lists/dicts/tuples and create loops/conditions.

What would be a good resource to gain knowledge on working with concepts that are new to me? Things I regularly see and don't understand are __init__ or __main__ funtions, when I should use classes or how to create a decent structure in my code. Currently I create a few functions and below them I write loops/conditions which use them. When looking at code from others I usually see them split over multiple files.

I'm working with python3.

You actually want to know two things:
0. How to write pythonic code;
Solution: Read Fluent Python. It is by far the best intermediate-level book I know of. You don't have to read it cover to cover; read the first few chapters and then skip around to stuff that interests you. As you learn more, you can go back to the more advanced topics. Plus, it's an excellent reference.

1. How to organize your code in general.

Solution: this is a little tougher! I would recommend Clean Code by Robert C. Martin. This is the best book I know of on the topic. It's about Java, but the concepts are universal. Honestly, though, it's just going to take a lot of practice.

Eela6 fucked around with this message at 14:51 on Jun 1, 2017

pangstrom
Jan 25, 2003

Wedge Regret
Probably sounds like faux outrage but I'm really just curious: is starting bullet lists for human consumption with element zero something programmers routinely do?

Eela6
May 25, 2007
Shredded Hen

pangstrom posted:

Probably sounds like faux outrage but I'm really just curious: is starting bullet lists for human consumption with element zero something programmers routinely do?

I do it, because otherwise my mind will snap back to one-based indexing and my code will start breaking on me. Years of MATLAB left permanent damage.

Cingulate
Oct 23, 2012

by Fluffdaddy

pangstrom posted:

Probably sounds like faux outrage but I'm really just curious: is starting bullet lists for human consumption with element zero something programmers routinely do?
I thought that's a thing people do to imply point zero is what you should have done already in the first place or something like that. Like, utter basics.

sugar free jazz
Mar 5, 2008

New to programming and python, and I made my first thing! Used Selenium to automate going into LexisNexis, then download all of the newspaper articles published that day from a few major newspapers that mention a couple different countries! It's janky, it's messy, but it actually does the thing I want it to which freaking owns. My low point was where I couldn't figure out how to deal with a few different frames on a page, and for a bit my workaround was send_keys((Keys.TAB * 57) + Keys.ENTER) to navigate to a button and click it yeeeesh. I feel like a lil kid and I wanna print it out and stick it on my refrigerator

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Has anybody dabbled with dependency injection with Python? I have reason to implement something like an API for getting implementations of APIs, and DI sounds like the way to go. I especially think back to how Angular managed stuff like this.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!

Cingulate posted:

I thought that's a thing people do to imply point zero is what you should have done already in the first place or something like that. Like, utter basics.

That's how I'd use a 0th level bullet list too. I translate it in my head as "this is the pre requisite."

Rocko Bonaparte posted:

Has anybody dabbled with dependency injection with Python? I have reason to implement something like an API for getting implementations of APIs, and DI sounds like the way to go. I especially think back to how Angular managed stuff like this.

I feel like people are gonna jump in and say "no don't do that," based on responses to other things I've asked about. But I don't really understand what you're asking though.

LochNessMonster posted:

So I've been messing around with Python for a few months but I feel I'm stuck at a level slightly above complete beginner. I know how to work with lists/dicts/tuples and create loops/conditions.

What would be a good resource to gain knowledge on working with concepts that are new to me? Things I regularly see and don't understand are __init__ or __main__ funtions, when I should use classes or how to create a decent structure in my code. Currently I create a few functions and below them I write loops/conditions which use them. When looking at code from others I usually see them split over multiple files.

I'm working with python3.

Sup me from the past.

Re: code organization, i think starting off with everything in a single file is the best way to go. That way everything is right there and easy to jump around to (I'm aware IDES and ctags makes this a nonissue but there's a physiological factor of knowing everything is right here that helps I think), and then splitting things off into their own files when the need arises.

LochNessMonster
Feb 3, 2005

I need about three fitty


Dominoes posted:

__init__ is boilerplate used in classes. Its most typical use is to convert arguments that are supplied when creating instances of the class into class-scope 'self' variables. Ie:

__main__ is boilerplate used when executing your file directly. ie:

Python code:

if __name__ == "__main__":
    run_all()

The snake example makes it a bit more comprehensible but I have no clue on what use the __main__ boilerplate would be. Couldn't you just end your code with runall()?


Eela6 posted:

You actually want to know two things:
0. How to write pythonic code;
Solution: Read Fluent Python. It is by far the best intermediate-level book I know of. You don't have to read it cover to cover; read the first few chapters and then skip around to stuff that interests you. As you learn more, you can go back to the more advanced topics. Plus, it's an excellent reference.

Thanks for the suggestion. I'll pick up a copy and start reading and practising.

Dominoes
Sep 20, 2007

Good point I missed: If you run_all() at the end without __main__, your code will run whenever it's imported. __main__ will only run if you load your script standalone.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
When you run a script, either directly or by importing it, it executes all the code in it from top to bottom.

But suppose you wanted to import this as a module. If you had a runall() function defined and then called at the bottom, it would run when you import, which may not be the behavior you want. You might want to have different behavior if it's running as a script, or as a module.

Dex
May 26, 2006

Quintuple x!!!

Would not escrow again.

VERY MISLEADING!

sugar free jazz posted:

New to programming and python, and I made my first thing! Used Selenium to automate going into LexisNexis, then download all of the newspaper articles published that day from a few major newspapers that mention a couple different countries! It's janky, it's messy, but it actually does the thing I want it to which freaking owns. My low point was where I couldn't figure out how to deal with a few different frames on a page, and for a bit my workaround was send_keys((Keys.TAB * 57) + Keys.ENTER) to navigate to a button and click it yeeeesh. I feel like a lil kid and I wanna print it out and stick it on my refrigerator

congrats. selenium can be finicky depending on the sites you're dealing with, so having hacks like tabbing to the thing and sending ENTER isn't that weird tbh. in some cases it's significantly faster than searching multiple xpaths or whatever, just make sure you're raising some kind of sensible error if the thing breaks at that point!

MonkeyMaker
May 22, 2006

What's your poison, sir?

Rocko Bonaparte posted:

Has anybody dabbled with dependency injection with Python? I have reason to implement something like an API for getting implementations of APIs, and DI sounds like the way to go. I especially think back to how Angular managed stuff like this.

Tom Christie talked about doing this for API development for his new API Star project.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Rocko Bonaparte posted:

Has anybody dabbled with dependency injection with Python? I have reason to implement something like an API for getting implementations of APIs, and DI sounds like the way to go. I especially think back to how Angular managed stuff like this.

I've done some experiments in this direction, similar-ish to what Tom Christie talks about in that video MonkeyMaker just posted.

I think it has a lot of potential.

You'll find that a lot of DI/IoC stuff in Python is never called that because DI/IoC just comes naturally to python due to its dynamic nature with first class everything.

LochNessMonster
Feb 3, 2005

I need about three fitty


Practical example question about working with functions.

Say I want to talk to APIs and run a few calls against it. My idea would be to create a function with requests to set up a connection. After the connection would be established I'd like to use that to run several different functions I write for the different api calls I want to do.

How would I go about setting something like that up?

Space Kablooey
May 6, 2009


I think it depends on the protocols, IIRC.

If you are using HTTP/S you have to make one request per connection, but if you are using TCP or UDP you could make something like that, but I wouldn't know how with requests, but it shouldn't be too hard.

Tigren
Oct 3, 2003

LochNessMonster posted:

Practical example question about working with functions.

Say I want to talk to APIs and run a few calls against it. My idea would be to create a function with requests to set up a connection. After the connection would be established I'd like to use that to run several different functions I write for the different api calls I want to do.

How would I go about setting something like that up?

Sounds like what you want is a Session.

http://docs.python-requests.org/en/master/user/advanced/#session-objects

quote:

The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3's connection pooling. So if you're making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection).

Thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!

Tigren fucked around with this message at 22:26 on Jun 2, 2017

Adbot
ADBOT LOVES YOU

LochNessMonster
Feb 3, 2005

I need about three fitty


That sounds exactly like what I want, thank you!

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