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
Cyril Sneer
Aug 8, 2004

Life would be simple in the forest except for Cyril Sneer. And his life would be simple except for The Raccoons.

cum jabbar posted:

Replace that laptop with something permanently attached to the machine that runs a web server.


CarForumPoster posted:

Computer hardware is cheap compared to technician time so I’m assuming you’re putting a dedicated laptop on the hardware.

No, there aren't dedicated laptops attached to each machine. Yes, it would be easy if that was the case - hence the question.

To elaborate, these "machines" are really just things like scopes and VNAs. We have scripts for operating them in particular ways (along with collecting/recording data in certain ways). Our engineers have their own laptops and want to be able to use these tools freely.

Adbot
ADBOT LOVES YOU

CarForumPoster
Jun 26, 2013

⚡POWER⚡

Cyril Sneer posted:

No, there aren't dedicated laptops attached to each machine. Yes, it would be easy if that was the case - hence the question.

To elaborate, these "machines" are really just things like scopes and VNAs. We have scripts for operating them in particular ways (along with collecting/recording data in certain ways). Our engineers have their own laptops and want to be able to use these tools freely.

Could you draw a use diagram that has these components and some idea of what scripts are where?

It sounds like the current state of affairs is this:


And what you want is...??this??


IDGI, provide the specifics. What problem you tryin to solve, what does success look like?

ComradePyro
Oct 6, 2009

Cyril Sneer posted:

Hey guys. To what extend can these various web frameworks be used to control local hardware?

Let me set up the problem I'm trying to solve. I work in a facility where Technician Bob might want to interface with hardware X, and Technician Sam might want to interface with hardware Y. The way it works right now is someone like me physically accesses Bob's laptop and installs whatever Python Stuff (environment, scripts) is needed for interfacing with hardware X. Then, someone like me gets ahold of Sam's computer and installs whatever Python Stuff is needed for him to interface with hardware Y.

I was thinking it would be really cool if instead Bob, Sam, and whoever else could simply access an internal webapp that provided the necessary functionality. I know that you can control hardware via a web interface but (and I'm going to bungle the phrasing here) where I've seen this, its external hardware connected to a server, and the server provides a remote user access to the local hardware. What I'm thinking is a bit of an inversion of this -- user connects their laptop to the hardware, loads the appropriate site, and then that "remote" site enables control of the local hardware (all of this would be fully internal).

My coding background is primarily in DSP/algorithm development/embedded processing so this webapp stuff is all a bit foreign to me.

maybe I'm just a poorly-trained orangutan, and maybe I've spent a lot of time overcomplicating poo poo, but that sounds like writing software that you'd have to deploy in order to avoid having to deploy software. it sorta sounds like having to put the laptop in your hands is the main issue, is remote desktop not a thing that can happen?

I have a bad case of infatuation with solving business process issues using software.

StumblyWumbly
Sep 12, 2007

Batmanticore!
This might be similar? Our board assembly house tests the hardware with some code we provide, and getting them updates has been a pain for multiple reasons, so I set up some stuff so they can pull the latest version of whatever they need from GitHub.

The steps are install Git, run a. bat that sets up the username and password (a fine grained PAT with access just to that repo), and clones the repo with the tool they and other scripts they may need (eg to pull the latest repo). Should work well, but the meeting to get them running is tomorrow.

If you definitely need a web based answer, I think the computer would need something special installed so the web app can interface with the serial or USB or w/e

Cyril Sneer
Aug 8, 2004

Life would be simple in the forest except for Cyril Sneer. And his life would be simple except for The Raccoons.

CarForumPoster posted:

Could you draw a use diagram that has these components and some idea of what scripts are where?

It sounds like the current state of affairs is this:


And what you want is...??this??


IDGI, provide the specifics. What problem you tryin to solve, what does success look like?

The top is what we currently have. I'd elaborate though and say the scripts are really apps, featuring GUIs, that allow the user to interact with the hardware in various pre-defined ways.

The bottom is sort-of what I want (sorry, can't edit the diagram right now): instead of the functionality provided via the local Python app, its provided "in browser" via a web app.


I know you can do remote users connecting to a remote server to control remote-connected hardware (think all those cam sites where users can go in and control the camera).
I want a remote user connecting to a remove server to control locally-connected hardware.

StumblyWumbly posted:


If you definitely need a web based answer, I think the computer would need something special installed so the web app can interface with the serial or USB or w/e

I think this is what it all hinges on. Seems like there's no way for a app-in-browser to interact with the hardware on which its running.

necrotic
Aug 2, 2005
I owe my brother big time for this!
The only way it could be provided “in browser” is if either the server had direct access to the device, or the user accessing the website was running a server the front end could communicate with (or the backend, if it could reach the laptop somehow).

If you can’t have a server connected to the device at all times, you need a middle man. The laptop becomes that middleman.

So you would still need to install XYZ to the laptop, for the website to even attempt this. And they would still have to be connected to the device directly.

LightRailTycoon
Mar 24, 2017
There is webUSB and webserial, but they are new, and not fully supported, and requires JavaScript drivers.

You could also run a web server on the laptop, and send it requests that correspond to commands, but this takes a lot of setup, and is a terrible security practice.

necrotic
Aug 2, 2005
I owe my brother big time for this!

LightRailTycoon posted:

There is webUSB and webserial, but they are new, and not fully supported, and requires JavaScript drivers.

You could also run a web server on the laptop, and send it requests that correspond to commands, but this takes a lot of setup, and is a terrible security practice.

I had completely forgotten they added the USB bullshit.

So that _is_ an option, assuming "connecting to the device" is not the main barrier here. But then yeah, its _very_ new and not all that well documented (or supported) yet I imagine.

Cyril Sneer
Aug 8, 2004

Life would be simple in the forest except for Cyril Sneer. And his life would be simple except for The Raccoons.

LightRailTycoon posted:

There is webUSB and webserial, but they are new, and not fully supported, and requires JavaScript drivers.

OooooOOh, this looks promising!

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
Isn't the easier way to go about this to stand up a basic web server on whatever's connected to the hardware? From there you can do all sorts of possibly dangerous things, such as running scripts/etc and you control it over http via a web browser? It's a security nightmare, but if it's limited enough it's not exactly rocket surgery.

Edit: honestly remote desktop seems to solve most of these problems now that I read over it.

FISHMANPET posted:

I think the problem you actually want to solve is a better way to deploy the instrument control software onto the users' devices, because it seems like the pain point you have is having to get hands on keyboard to set this stuff up. Engineering a web app using cutting edge java script is probably way more complicated and prone to failure than a better system for deploying the control software you have already.

Yeah I think this is probably accurate; it does sound like they're overcomplicating the problem.

Falcon2001 fucked around with this message at 04:41 on Jul 6, 2023

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
I think the problem you actually want to solve is a better way to deploy the instrument control software onto the users' devices, because it seems like the pain point you have is having to get hands on keyboard to set this stuff up. Engineering a web app using cutting edge java script is probably way more complicated and prone to failure than a better system for deploying the control software you have already.

boofhead
Feb 18, 2021

Yeah having worked years ago in a schools IT department, and now as a python developer, my (still very rookie) first reaction was that this doesn't sound like something you'd do in python, it's a domain/deployment question. So something I'd have dealt with on the domain or device level rather than kludging together some weird ad hoc stuff that's just going to be a nightmare to develop, implement, and maintain

But I am also very very tired so I don't know how much I actually understand the issue at hand

StumblyWumbly
Sep 12, 2007

Batmanticore!
My company sells sensors, and we have some open source Python libraries for configuring and interfacing with them, but we'd like to make some advanced wireless features that we can sell as subscription software, because that's how the world works now. It seems like the way to paywall a Python library for Windows would be to have a separate server running out of an install/EXE, which does the real work including handling the licensing and communicating with the device, and the Python library would just be an API interface with the local server. Qoitech (https://www.qoitech.com/) does this for automating an interface to their OTII power supplies, and it seems to work ok with some issues (RasPi support seems janky, folks keep forgetting to log out and lose their license, etc).

The issue with just doing it all in Python, of course, is our secrets are completely exposed.

Has anyone seen a better way to do this?

wolrah
May 8, 2006
what?
So you're saying you want to charge a subscription for a local device talking to a local network service that costs your company nothing ongoing?

If you're not incurring recurring costs but you want to charge them to your customers, that's bullshit.

It's fine to charge a subscription for a cloud service, for technical support, for ongoing software updates, etc. Things that actually have ongoing costs because they involve resources you're paying for, which you can then easily control access to.

Unless I'm reading you wrong what you want to do is not OK and should not be encouraged or assisted. Yes a bunch of lovely vendors already do it, but we don't need more of it in the world.

QuarkJets
Sep 8, 2008

StumblyWumbly posted:

My company sells sensors, and we have some open source Python libraries for configuring and interfacing with them, but we'd like to make some advanced wireless features that we can sell as subscription software, because that's how the world works now. It seems like the way to paywall a Python library for Windows would be to have a separate server running out of an install/EXE, which does the real work including handling the licensing and communicating with the device, and the Python library would just be an API interface with the local server. Qoitech (https://www.qoitech.com/) does this for automating an interface to their OTII power supplies, and it seems to work ok with some issues (RasPi support seems janky, folks keep forgetting to log out and lose their license, etc).

The issue with just doing it all in Python, of course, is our secrets are completely exposed.

Has anyone seen a better way to do this?

You said that the libraries are open source, so what secrets are you trying to protect? If you mean "secrets" as in authentication details then those shouldn't be in your codebase anyway.

StumblyWumbly
Sep 12, 2007

Batmanticore!

wolrah posted:

So you're saying you want to charge a subscription for a local device talking to a local network service that costs your company nothing ongoing?

If you're not incurring recurring costs but you want to charge them to your customers, that's bullshit.

It's fine to charge a subscription for a cloud service, for technical support, for ongoing software updates, etc. Things that actually have ongoing costs because they involve resources you're paying for, which you can then easily control access to.

Unless I'm reading you wrong what you want to do is not OK and should not be encouraged or assisted. Yes a bunch of lovely vendors already do it, but we don't need more of it in the world.
I don't want to get into the details, but this isn't "we'll sell you a computer but you pay each month if you want to use wi-fi". This would replace our cloud system in some places, give folks local control, and add new features to existing devices. Maybe we'll do a one time license, who knows. Plus we already offer ongoing support and development which is free right now.

There's a lot of details I'm not providing because this is just a noxious conversation to have. I hate capitalism too.

QuarkJets posted:

You said that the libraries are open source, so what secrets are you trying to protect? If you mean "secrets" as in authentication details then those shouldn't be in your codebase anyway.
That's true, we could just grab a key and use that key in the communication with the device. That's a much simpler idea. It will expose a lot of our packet and data format more clearly, but that should be fine.

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
Yeah I think this is why a lot of companies used to distribute hardware fobs.

I would look at it from a perspective of doing a cryptographic validation of some kind, but as others have mentioned, there's just going to be a risk that a determined customer rewrites the libraries to bypass those checks and uses their new 'hacked' versions instead.

Foxfire_
Nov 8, 2010

What is the threat model your trying to protect against?

Unless you're going to jump in to a bunch of DRM stuff, editing a straightforward license check out of an executable isn't particularly hard.

If you're selling widgets to businesses, your main defense is your license agreement and customers unwillingness to commit crimes (even if it's very unlikely they'll ever get caught), not technical difficulty

I would guess that it's not business worthwhile for you to do anything more than some trivial check that may be straightforward to bypass, but makes it explicit that the customer removing the "has_valid_license()" call is being naughty.

StumblyWumbly
Sep 12, 2007

Batmanticore!
The main threat would be folks trying to access features on the device that they don't have a license for, so we can do the actual check on the device, which is very secure.

I'm not super strong on cryptography, it feels like we might be doing enough out in the open that the customer can figure out our magic crypto numbers. We'll look into that, but I'm not super worried about it since our customers are mostly businesses.

spiritual bypass
Feb 19, 2008

Grimey Drawer
You'll probably need to provide a token signed with your own company's certificate. API users would provide this credential when making a request, then the device could use the certificate's public key to validate the signature. This keeps all actual secrets still inside the company.

As a bonus, you could frame it as "enhanced security" since you now have an authentication system thay didn't exist before

Son of Thunderbeast
Sep 21, 2002
Imagine you're debugging and you come across this for the first time since you'd written it, and you're trying to remember what the thing you were tracking is doing at this point, at any given point

code:
for this_item in this_item_list:
	item_list.append(this_item)
It's simple but all the items have me forgetting what I'm doing by the time I reach the last this_item

I'm keeping it because it amuses me

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

Son of Thunderbeast posted:

Imagine you're debugging and you come across this for the first time since you'd written it, and you're trying to remember what the thing you were tracking is doing at this point, at any given point

code:
for this_item in this_item_list:
	item_list.append(this_item)
It's simple but all the items have me forgetting what I'm doing by the time I reach the last this_item

I'm keeping it because it amuses me

quote:

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

Too loving true.

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
I'm about to start working on a new project shortly and I'm trying to get my team into using Pytest instead of the built in UnitTest framework. I like it a lot more and it seems universally liked as well, but a lot of the folks I'm working with haven't done a lot of Python, so I'm trying to figure out good arguments for using it. Unfortunately I'm kind of struggling with coming up with good advantages; especially since we already use the pytest runner, which happily handles Unittest. Anyone have any good suggestions?

FWIW: I'm in a position to definitely dictate what we'll be using for the test suite but I'd prefer to be like 'oh yeah we're using this new thing you haven't done before because XYZ' instead of just 'here it is, deal'

monochromagic
Jun 17, 2023

Falcon2001 posted:

I'm about to start working on a new project shortly and I'm trying to get my team into using Pytest instead of the built in UnitTest framework. I like it a lot more and it seems universally liked as well, but a lot of the folks I'm working with haven't done a lot of Python, so I'm trying to figure out good arguments for using it. Unfortunately I'm kind of struggling with coming up with good advantages; especially since we already use the pytest runner, which happily handles Unittest. Anyone have any good suggestions?

FWIW: I'm in a position to definitely dictate what we'll be using for the test suite but I'd prefer to be like 'oh yeah we're using this new thing you haven't done before because XYZ' instead of just 'here it is, deal'

As someone who has successfully convinced a team of developers to the wonders of pytest, I feel I can answer this question.

The big thing about pytest for me is ergonomics. Both simply in terms of code simplicity - functions vs. subclasses, assert support without having to use assertEquals etc., but also the extremely powerful fixtures and parametrization support out of the box.

The best argument I provided was simply refactoring part of our existing test suite to pytest, and showing how much simpler and more expressive the tests ended up being. I'm thinking a similar approach might work for you - show just a small example from on of our UnitTest test suites refactored to using pytest, and in my experience, developers will jump on things that make their lives easier, which pytest for sure does in my opinion. Then, you can simply say "let's use [new project] to learn pytest in-depth."
That is probably how I'd go about it, anyway.

Data Graham
Dec 28, 2009

📈📊🍪😋



Depending on the kind of code you need to test, parametrization might even be a non-negotiable feature. Like if you have a complex calculation method you want to test with 100 different sets of inputs. One test function with 100 clearly marked inputs is way better than 100 nearly identical and super redundant test functions.

monochromagic
Jun 17, 2023

Data Graham posted:

Depending on the kind of code you need to test, parametrization might even be a non-negotiable feature. Like if you have a complex calculation method you want to test with 100 different sets of inputs. One test function with 100 clearly marked inputs is way better than 100 nearly identical and super redundant test functions.

Oh absolutely, and if you want to get fancy with it you can do absolutely amazing property-based testing things with Hypothesis.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Here's a little list in no particular order, none of them having anything to do w/ the test runner:

1. The unittest lib is old and crusty (low hanging example: camel-case method names). Pytest is not crusty, and its interface is pythonic. Using pytest will guide your coworkers who are less familiar w/ Python toward better practices elsewhere in the code.
2. Strong support for testing without classes, fixtures eliminating the need for setup/teardown methods being the main thing here. (They are a little bit magical though, which is actually points against pytest from me).
3. The focus on using python-native assert, rather than a bunch of methods I'm constantly having to look up, is liberating.
4. Pytest provides some helpful stuff like the "pytest.raises" context manager that I reach for all the time. Unittest offers self.assertRaises for this, but I believe the pytest version has a wrapper which is easier to work with than the raw exception object that assertRaises gives you.

You can make unittest work, there's nothing catastrophically wrong with it, but I've never been on a team who didn't see pytest as the obvious choice. It's just next to 0 lift for quite a bit of benefit, particularly if you're already using the runner.

The March Hare fucked around with this message at 22:45 on Jul 8, 2023

Data Graham
Dec 28, 2009

📈📊🍪😋



It's annoying that Django tests are built on unittest. I wonder if they're ever planning on moving away from that?

lazerwolf
Dec 22, 2009

Orange and Black

Data Graham posted:

It's annoying that Django tests are built on unittest. I wonder if they're ever planning on moving away from that?

There’s a pytest Django module

pmchem
Jan 22, 2010


what's the easiest way to plot county-by-county geographic data in python these days along with some good data sources?

like say I want to plot rainfall, or average seasonal temperatures, and mosquito density for a county-by-county map of the USA. counties colored by amount. googling this leads down many not entirely useful rabbit holes.

are arcgis and geopandas the current hotness? does arcgis only interface with... arcgis, which I generally understand is a nonfree api?

CarForumPoster
Jun 26, 2013

⚡POWER⚡

pmchem posted:

what's the easiest way to plot county-by-county geographic data in python these days along with some good data sources?

like say I want to plot rainfall, or average seasonal temperatures, and mosquito density for a county-by-county map of the USA. counties colored by amount. googling this leads down many not entirely useful rabbit holes.

are arcgis and geopandas the current hotness? does arcgis only interface with... arcgis, which I generally understand is a nonfree api?

I haven’t looked in three years but back then county by county was a bitch. I think I ended up doing it by zip assigning certain zip codes to certain counties based on a spreadsheet I found of that. IIRC some zips spam multiple counties but it was good enough for the problem I had then.

Big Dick Cheney
Mar 30, 2007
I like plotly. I usually do zip codes/census tracts but it would work for counties too. Just need the right shapefiles.

Jose Cuervo
Aug 25, 2004

pmchem posted:

what's the easiest way to plot county-by-county geographic data in python these days along with some good data sources?

like say I want to plot rainfall, or average seasonal temperatures, and mosquito density for a county-by-county map of the USA. counties colored by amount. googling this leads down many not entirely useful rabbit holes.

are arcgis and geopandas the current hotness? does arcgis only interface with... arcgis, which I generally understand is a nonfree api?

I have used geopandas in the past and found it very powerful (for creating static maps, if you want interactive maps I don't know).

County shapefiles can be found here at different resolutions: https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html

Perhaps some of the data you need can be found here: https://www.ncei.noaa.gov/access/monitoring/climate-at-a-glance/county/mapping

Although they have the data plotted at the county level already, so perhaps you can just use those maps if they are plotting the data you want plotted (they allow you to export as a png)?

pmchem
Jan 22, 2010


Jose Cuervo posted:

I have used geopandas in the past and found it very powerful (for creating static maps, if you want interactive maps I don't know).

County shapefiles can be found here at different resolutions: https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html

Perhaps some of the data you need can be found here: https://www.ncei.noaa.gov/access/monitoring/climate-at-a-glance/county/mapping

Although they have the data plotted at the county level already, so perhaps you can just use those maps if they are plotting the data you want plotted (they allow you to export as a png)?

yeah, that second link basically contains the sort of plots I want to make! except I want to also plot other county-level data series of my own (hence plot it all on my own) and perform other local analysis.

Biffmotron
Jan 12, 2007

For these types of maps I really like Plotly choropleths. It mostly just works, and plotly gets you a bunch of nice quality of life features with being able to zoom and pan plots in a notebook instead of fiddling with the axes.

Hed
Mar 31, 2004

Fun Shoe
I've inherited some code that is Django/Django Rest Framework, but thought this is more of a general Python question.

I have a function that takes a DRF object (type Job) and needs to get dispatched to a JobRunner of the correct type. In the middle is a "figure out what kind of serialization" that has business logic. I don't love the design, but primarily want to make this if..elif..else pattern much easier. How should I think about making the below less tedious. Some kind of metaprogramming or Enum that defines the mapping in a straightforward way, then just looking at the dict/mapping?

Here's an example of what's going on currently:

Python code:
def get_serializer_class(job):
    if not job.search_type:
        logger.error(f"no search type: {job}")
        return JobSerializer
    if job.site == "site1.tld":
        if job.search_type == "search_type_1":
            return Site1SearchTypeOneSerializer
        elif job.search_type == "search_type_2":
            return Site2SearchTypeTwoSerializer
        elif job.search_type == "search_type_3":
            return Site3SearchTypeThreeSerializer
    elif job.site == "site2.tld":
        if job.search_type == "search_type_1":
            return Site2SearchTypeOneSerializer
        elif job.search_type == "search_type_2":
            return Site2SearchTypeTwoSerializer
    # ... repeat 15 times ...
    else:
        return JobSerializer

LightRailTycoon
Mar 24, 2017
Use a dict, with the job.search_type as the key, and the serializer as the value.

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
Looks like maybe a nested dict to handle the second if/else block there but still, same concept.

Data Graham
Dec 28, 2009

📈📊🍪😋



What I like to do in situations like this, if at all possible, is to make use of inheritance. The get_serializer_class method is meant to be overridden in a subclass of whatever your main view class is; so if your view class is MyJobViewClass, you would have subclasses called MySite1JobViewClass and MySite2JobViewClass and so on like:

Python code:
class MySite1JobViewClass(MyJobViewClass):

    def get_serializer_class(self):
        return Site1SearchTypeOneSerializer


class MySite2JobViewClass(MyJobViewClass):

    def get_serializer_class(self):
        return Site2SearchTypeOneSerializer
And so on. And you would use some other logic higher up to determine which of the JobViewClasses to call, i.e. rather than explicitly keying off job.search_type you might have routes specifically directing one search_type to one view class and another search_type to another view class.

That way you avoid having any kind of if-elif-elif-etc or switch-type structure which is really ugly to my eyes and hard to maintain. Django and DRF are meant to leverage subclassing a lot so that you can differentiate your routes at the class level and then subclass these helper methods like get_serializer_class in tiny little one-liners that are highly expressive and maintainable.

It also means you can leverage mixins and base classes, which means your helper methods can call their superclass methods:

Python code:
class JobSerializerMixin:
    
    def get_serializer_class(self):
        return JobSerializer


class MySite3JobViewClass(JobSerializerMixin, MyJobViewClass):
    def get_serializer_class(self):
        serializer = super().get_serializer_class(self)
        if blah == 'foo':
            return Site3SearchTypeOneSerializer
        return serializer
And stuff like that.

This may or may not be helpful (I see you're using job as your class variable instead of self so you may be doing something I'm not seeing as I'm shooting from the hip without proofreading, but this is the pattern I like to use if it seems to fit your use case.

Data Graham fucked around with this message at 21:49 on Jul 15, 2023

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

I was going to recommend dictionary dispatch, like a few other posters already did. This is much cleaner and easier to read than using classes and inheritance in my opinion.

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