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
yippee cahier
Mar 28, 2005

hooah posted:

I'm writing some code to break up a large file into smaller files, but I'm having a hard time wrapping my head around the checks. Here's what the large file's format looks like, with specific markers at the end of each line:

I want to group all the aa's together, then the b1's, then c1's, etc. So far, my code works fine until the ab's, which get grouped with the e1's. Here's some pseudocode of what I'm doing:

I tried putting the final else at the top of the if, but that caused the aa1 line to get written to a file, since the aa1 marker was different than the garbage marker.

I think maybe something's being lost in your pseudocode translation. My approach would be to iterate over all the lines, break off the marker, then build up a dict with keys of marker and a list value containing your lines, using setdefault():
code:
>>> records = {}
>>> records.setdefault('marker',[]).append('line1')
>>> records.setdefault('marker',[]).append('line2')
>>> records.setdefault('marker2',[]).append('line3')
>>> records.setdefault('marker2',[]).append('line4')
>>> records
{'marker': ['line1', 'line2'], 'marker2': ['line3', 'line4']}
>>> 
Then you can iterate over your dict's keys and dump out the contents of the associated list to your files. Breaking it up like this will handle cases where a marker appears out of order in your input and allow you to do stuff like sort the output list before writing it, etc.

This is ripe for use of generator one liners as well.

yippee cahier fucked around with this message at 18:16 on Jul 18, 2015

Adbot
ADBOT LOVES YOU

hooah
Feb 6, 2006
WTF?
I had no idea Matlab had dictionaries. Can the dictionary be sorted or accessed in a sorted manner by the first element in the list? That should help immensely, thanks.

nielsm
Jun 1, 2009



hooah posted:

I had no idea Matlab had dictionaries. Can the dictionary be sorted or accessed in a sorted manner by the first element in the list? That should help immensely, thanks.

I'm pretty sure sund's example is Python, not Matlab. You never specified a language in your original question, but your pseudocode looks suspiciously close to actual Python.

hooah
Feb 6, 2006
WTF?
Oh, whoops. I think I'd typed Matlab at one point and then deleted it. I just got done being a Python TA, so it must be leaking. I've already done what I have in Matlab, so continuing that would be nice, but not necessary.

I just realized that a dictionary probably won't work, since the markers for b1 and b2 aren't actually different yet they need to be written to different files (that was poor explanation on my part).

hooah fucked around with this message at 00:38 on Jul 19, 2015

yippee cahier
Mar 28, 2005

Haha, sorry, I assumed pseudocode replacement for more complicated operations and Python. Matlab? Can't help you there.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Matlab actually did add a dictionary type at some point. It's somewhat limited but supports all of the basic operations.

Dumlefudge
Feb 25, 2013
Is there any sensible way to approach working with a library that is downright insane? The current project I'm working on required working with an API (with a Java wrapper library to 'ease' development). The API itself is quirky in its own right, but the library is doing some rather odd things in general. There are at least 5 ways for this library to fail with a NullPointerException thrown somewhere deep inside the library. Other times, it will return an empty response object.

As a result, I'm somewhat lost as to how to tackle this - the approach I'm currently taking is to have a general-purpose error handler to catch the NPE's and malformed objects and identify the possible sources of error (e.g. bad credentials, wrong IP, etc.) and point to the logs in order to locate the actual cause of failure, followed by use-case-specific error handling, but "Just read the drat logs" seems like a bit of a cop-out.

Kuule hain nussivan
Nov 27, 2008

I'm going to start writing my first project with a GUI. I'll be writing in Java, but how should I implement the frontend? Is Swing something that people actually use, or should I go with something else, like javascript instead?

Also, can anyone recommend a guide on what's good practice when programming a GUI. I know the basic stuff like keeping the GUI separate from the backend logic, but I'm sure there's tons of ways of implementing it that are frowned upon.

Thermopyle
Jul 1, 2003

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

Dumlefudge posted:

Is there any sensible way to approach working with a library that is downright insane? The current project I'm working on required working with an API (with a Java wrapper library to 'ease' development). The API itself is quirky in its own right, but the library is doing some rather odd things in general. There are at least 5 ways for this library to fail with a NullPointerException thrown somewhere deep inside the library. Other times, it will return an empty response object.

As a result, I'm somewhat lost as to how to tackle this - the approach I'm currently taking is to have a general-purpose error handler to catch the NPE's and malformed objects and identify the possible sources of error (e.g. bad credentials, wrong IP, etc.) and point to the logs in order to locate the actual cause of failure, followed by use-case-specific error handling, but "Just read the drat logs" seems like a bit of a cop-out.

What I've done in the past is write a wrapper with a sane API for the insane library. The wrapper handles all the bullshit the insane library does. It can retry operations or whatever it is that needs to be done.

Then forget the insane library exists.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Kuule hain nussivan posted:

I'm going to start writing my first project with a GUI. I'll be writing in Java, but how should I implement the frontend? Is Swing something that people actually use, or should I go with something else, like javascript instead?

Swing is what's usually used to create a GUI for a java application running on your system, yes. There are a few others (AWT (deprecated in favor of Swing), SWT, JavaFX) but Swing is the most common.

ExcessBLarg!
Sep 1, 2001

Kuule hain nussivan posted:

I'm going to start writing my first project with a GUI. I'll be writing in Java, but how should I implement the frontend? Is Swing something that people actually use, or should I go with something else, like javascript instead?
Swing is quite popular so it has good third party support (if you need a third party component), but it's also an older toolkit and so doesn't make use of modern "best practices" such as separating styling and markup from code. JavaFX is the Oracle-blessed successor to Swing which has all the modern bells and whistles: styling with CSS, markup with FXML, webviews, extensive media support, etc. However, it's not as mature as Swing and not well supported prior to Java 8. I'd also say that JavaFX isn't as popular now as Swing was in its heyday as folks building new UIs today usually don't want to exclude popular mobile/tablet platforms (iOS and Android), which JavaFX currently does.

If you don't mind targeting Java 8, I'd take a close look at JavaFX and make sure that it's capabilities meet your needs out of the box. If it does, I might try going that route as it's fairly modern conceptually. If you're looking to remain compatible with older versions of Java, or make use of the existing third-party libraries or code, I might stick with Swing given its overall popularity.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Worth clarifying: AWT is Java's graphics foundation. Swing and anything else that doesn't punt to JNI calls is built on top of AWT. If you're using Swing, you will see AWT classes from time to time. AWT also encompasses Java2D, a quite serviceable canvas-style drawing API which is hardware-accelerated on many platforms. If you need to do something like custom graphing or a 2d game there's a good chance it can do what you need out of the box.

goodness
Jan 3, 2012

When the light turns green, you go. When the light turns red, you stop. But what do you do when the light turns blue with orange and lavender spots?
.

goodness fucked around with this message at 06:42 on Dec 11, 2018

Peristalsis
Apr 5, 2004
Move along.

goodness posted:

I am looking to learn some code and completely new to all this. It would primarily just be used for small programs/web design. A language that would be useful to help program automated garden/aquarium systems, maybe a little robotics, create a web page to sell a product/show info, pair with electrical work to run a self-sufficient power grid, etc. Any suggestions where to start?

I think Python is many peoples' default suggested first language these days. It's free, ubiquitous, and widely used and supported. It should be able to handle any of the things you mention here, though you might want/need to add in a web framework (like Django) to do web programming.

Peristalsis
Apr 5, 2004
Move along.
Here's an easy question to start off the day.

I'm pretty new to JQuery, and fumbling around with the selector. I'm not getting exactly what I want from a selection, but I'm not sure how to inspect the results of my query to see which elements are selected and which are not. I've been using the alert() function to display the size of the returned collection, and I can also use toArray() and then print out the join() of the resulting array, but this only shows me that it's an array of HTML Input Elements, not which elements. I tried using the name property of each object, but that doesn't return anything.

What's the best way to see some identifying information on the elements being returned? I've been Googling around, getting close to the answer, but I just want to move on with my life at this point.

nielsm
Jun 1, 2009



Peristalsis posted:

Here's an easy question to start off the day.

I'm pretty new to JQuery, and fumbling around with the selector. I'm not getting exactly what I want from a selection, but I'm not sure how to inspect the results of my query to see which elements are selected and which are not. I've been using the alert() function to display the size of the returned collection, and I can also use toArray() and then print out the join() of the resulting array, but this only shows me that it's an array of HTML Input Elements, not which elements. I tried using the name property of each object, but that doesn't return anything.

What's the best way to see some identifying information on the elements being returned? I've been Googling around, getting close to the answer, but I just want to move on with my life at this point.

Use the debugger and interactive console in your browser. IIRC both Firefox, Chrome and IE activate their debugging tools with F12.

You can type Javascript directly into the console and have it evaluated line by line, then inspect the results. It all runs in the context of the page you opened the debugger from, so you can work on the live site like that.

Since SA uses jQuery too, I can open the debugger right on this page, choose the Console tab, and enter:
$("span")
I then get an output similar to this:
Object { 0: <span.nav_new>, 1: <span.mainbodytextlarge>, 2: <span.smalltext>, 3: <span.smalltext>, 4: <span>, 5: <span>, 6: <span>, 7: <span>, 8: <span>, 9: <span>, 25 more… }
Except that it's interactive, I can point to the elements listed and have them highlited on the page, and I can click them to inspect them further.

And yes you can also use variables from the console.

Peristalsis
Apr 5, 2004
Move along.

nielsm posted:

Use the debugger and interactive console in your browser. IIRC both Firefox, Chrome and IE activate their debugging tools with F12.

You can type Javascript directly into the console and have it evaluated line by line, then inspect the results. It all runs in the context of the page you opened the debugger from, so you can work on the live site like that.

Since SA uses jQuery too, I can open the debugger right on this page, choose the Console tab, and enter:
$("span")
I then get an output similar to this:
Object { 0: <span.nav_new>, 1: <span.mainbodytextlarge>, 2: <span.smalltext>, 3: <span.smalltext>, 4: <span>, 5: <span>, 6: <span>, 7: <span>, 8: <span>, 9: <span>, 25 more… }
Except that it's interactive, I can point to the elements listed and have them highlited on the page, and I can click them to inspect them further.

And yes you can also use variables from the console.

Cool, thanks.

Is there a property of the collected elements I could use to print their names/id, if I did want to throw them into a popup?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Peristalsis posted:

Cool, thanks.

Is there a property of the collected elements I could use to print their names/id, if I did want to throw them into a popup?

Selectors use the array indexing syntax to look up individual raw elements

JavaScript code:
$('.classname')[0] // raw DOM element not wrapped by a jQuery selector
//which means it has whatever properties the browser happens to support
or you can use .eq() or add :eq() to your selector's query to get a selector for an individual element (https://api.jquery.com/eq/ and https://api.jquery.com/eq-selector/)

JavaScript code:
$('.classname').eq(0) //is equivalent to
$('.classname:eq(0)') //and both return selectors that support the normal jQuery API
To get the ID of a selector's first element, you'd use .attr('id') (https://api.jquery.com/attr/). This is distinct from but sort-of related to .prop() (https://api.jquery.com/prop/).

To iterate over the items in a selector, you can treat it like an array and use a regular for loop because selectors have a length property that tells you how many items they matched.

JavaScript code:
for (var i = 0; i < $elector.length; ++i) {
    console.log($elector.eq(i).attr('id')); //alerts are inferior
    console.log($elector[0].id); //just get used to hitting F12
}
Or you can use .each() (https://api.jquery.com/each/)

JavaScript code:
$elector.each(function () {
    console.log($(this).attr('id'));
    console.log(this.id);
});
Also there's a thread for this http://forums.somethingawful.com/showthread.php?threadid=2971614

TacoHavoc
Dec 31, 2007
It's taco-y and havoc-y...at the same time!
I am having a problem that involves scoping and Javascript and I'm having some issues figuring it out.

code:
onSearchButtonTap: function(button, e, eOpts) {
    WifiWizard.getCurrentSSID(this.ssidHandler, this.ssidFail);
},

processNetwork: function(ssidString) {
    //check the name and whatnot
},

ssidHandler: function(obj) {
    console.log('SSID returned');
    console.log(obj);
    this.processNetwork(obj);  // -> this doesn't work
    //TypeError: undefined is not a function (evaluating 'this.processNetwork(b)')
}
I tap the search button, the WiFiWizard.getCurrentSSID method fires, and the ssidHandler outputs the SSID and the obj variable to the console, but then barfs on trying to call the processNetwork. How should I be calling processNetwork?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
JavaScript is weird. Use this.ssidHandler.bind(this) when you can the wizard.

TacoHavoc
Dec 31, 2007
It's taco-y and havoc-y...at the same time!

Suspicious Dish posted:

JavaScript is weird. Use this.ssidHandler.bind(this) when you can the wizard.

Holy crap you're the best. I was playing around with .apply but apparently not doing that right either. I didn't realize .bind was a thing.

BlackMK4
Aug 23, 2006

wat.
Megamarm
Where does one start if they want to get into generating 2d/3d graphics and they are coming from a background of building console apps / SAAS stuff in Java/Python?

the talent deficit
Dec 20, 2003

self-deprecation is a very british trait, and problems can arise when the british attempt to do so with a foreign culture





BlackMK4 posted:

Where does one start if they want to get into generating 2d/3d graphics and they are coming from a background of building console apps / SAAS stuff in Java/Python?

gaming? i'd try unity. or unreal engine. both are basically free (until you start making money with them)

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

BlackMK4 posted:

Where does one start if they want to get into generating 2d/3d graphics and they are coming from a background of building console apps / SAAS stuff in Java/Python?

What does "generating 2d / 3d graphics" mean? Are you looking to do UI programming, image processing, offline rendering, real-time gaming, or what?

Sab669
Sep 24, 2009

Any thoughts on how to cleanly handle support for multiple versions of records in a web application?

My new job gets government mandated changes annually and the code is a loving mess. "Info1", " Info2", "Info3" are all almost identical forms except each has a few different controls based on the record being accessed. And almost every page in the app has two or three variants. It's not always 1 form per record version, one form can handle more than one version in some cases. But it's only going to get bigger and uglier.

The code just has a poo poo ton of IF statements comparing string literals for the different versions in almost every link to another page, determining which version of a page to send the user to.

"V1.10.1", " V1.01A", "V1.2", " V1.3" in order of oldest to newest.

I'd love to, at the very least, replace it allll with an enum instead but who has the time for that much of a change. That'd be easier to compare and add to going forward, I think.

Luckily String.Compare (string, string) seems to work for our needs, so I'm going back and replacing every strVer == "xyz" with that, but goddamn it's messy. There's got to be a better way.

Any ideas, or suggestions for what to read about?

ASP / VB.NET fyi

Sab669 fucked around with this message at 13:37 on Jul 23, 2015

BlackMK4
Aug 23, 2006

wat.
Megamarm

Suspicious Dish posted:

What does "generating 2d / 3d graphics" mean? Are you looking to do UI programming, image processing, offline rendering, real-time gaming, or what?

Image processing - something like generating a voronoi diagram on a jpg with points. Simple 2d pong. The usual raytraced scenes.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.
So I am trying to set up an NFS on my linux box (running fedora) so I can connect to it from my MAC. The fedora is a virtual machine, on a bridge connection. I can connect and ssh into my fedora box no problem.

The user I am using on my fedora box that does everything has a uid and gid of 1001. My mac user has a uid of 101, and gid of 20.

This is in my /etc/exports: "/home *(rw,insecure,sync,no_root_squash,no_all_squash,anonuid=1001,anongid=1001)"

I then restart the nfs-server.service, but my mac just won't connect to it. When I connect to my nfs server I use the mac finder GUI and enter "nfs://ems@10.211.55.9/home" (ems is the user on the fedora system). When I do this, is my client going to connect with the uid and gid of ems, or will it connect with the uid and gid of my mac's user?

Regardless of that question, this does not work and I cannot connect to my fedora box, any help greatly appreciated.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

BlackMK4 posted:

Image processing - something like generating a voronoi diagram on a jpg with points. Simple 2d pong. The usual raytraced scenes.

Pick one. Those are all entirely different things with different codebases, strategies, techniques and things to learn.

BlackMK4
Aug 23, 2006

wat.
Megamarm

Suspicious Dish posted:

Pick one. Those are all entirely different things with different codebases, strategies, techniques and things to learn.

Okay, where is the entry to the voronoi diagram from an image? The latter two seem to be gated by OpenGL (as one method).

TheresaJayne
Jul 1, 2011

Knifegrab posted:

So I am trying to set up an NFS on my linux box (running fedora) so I can connect to it from my MAC. The fedora is a virtual machine, on a bridge connection. I can connect and ssh into my fedora box no problem.

The user I am using on my fedora box that does everything has a uid and gid of 1001. My mac user has a uid of 101, and gid of 20.

This is in my /etc/exports: "/home *(rw,insecure,sync,no_root_squash,no_all_squash,anonuid=1001,anongid=1001)"

I then restart the nfs-server.service, but my mac just won't connect to it. When I connect to my nfs server I use the mac finder GUI and enter "nfs://ems@10.211.55.9/home" (ems is the user on the fedora system). When I do this, is my client going to connect with the uid and gid of ems, or will it connect with the uid and gid of my mac's user?

Regardless of that question, this does not work and I cannot connect to my fedora box, any help greatly appreciated.

Make sure your virtual machine gets its own IP address on the lan , also make sure that the fedora firewall is turned off.

TheresaJayne fucked around with this message at 05:52 on Jul 24, 2015

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

BlackMK4 posted:

Okay, where is the entry to the voronoi diagram from an image? The latter two seem to be gated by OpenGL (as one method).

What? A voronoi diagram is generated from a set of points -- it graphs the entire 2D plane with the other points closest to that point. I don't know what generating it from an image would mean.

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.

Suspicious Dish posted:

What? A voronoi diagram is generated from a set of points -- it graphs the entire 2D plane with the other points closest to that point. I don't know what generating it from an image would mean.

Using the pixels of an image as the point set, and using color difference as the distance metric.

unixbeard
Dec 29, 2004

BlackMK4 posted:

Where does one start if they want to get into generating 2d/3d graphics and they are coming from a background of building console apps / SAAS stuff in Java/Python?

https://www.processing.org/

raminasi
Jan 25, 2005

a last drink with no ice

Sinestro posted:

Using the pixels of an image as the point set, and using color difference as the distance metric.

How do you embed that into the 2D plane though? I'm not speaking rhetorically, I've never heard of this.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
This is about writing GUIs with Unity, but I think it is a more general problem not worth putting up in the game programming thread. I'm wondering what are some things I should think about doing with this since the GUI system there is just so primitive.

Any GUI I'm implementing just has one method that Unity calls, basically in a poll loop, with one event at a time. Common events I worry about are painting the GUI, keyboard stuff, and mouse stuff. If you just go through it procedurally without any design considerations, you wind up with a bunch of conditionals on all those event types to do all the random bits you want to in the custom GUI. The method that Unity calls can then hit quadruple digits of lines of code without paying too much attention.

I figured out last night that wasn't good enough at least for mouse stuff. If had an operation where I wanted to extend a wall preview I intended to place when dragging the mouse across an area. So I was checking when dragging ended and a MouseUp event showed up. However, a repaint could happen in the middle and make things a mess. I ended up creating a little mouse helper object that looked at the event each loop and set a bunch of variables related to the mouse being down, up, dragging, and what it was doing previously. This took care of having a bunch of annoying variables flipping around inside the giant method call.

I'm assuming that's another wheel reinvented, so I'm wondering if there's a nice compendium of lower-level GUI patterns that tend to crop up in this kind of thing. Normally I'm working with higher-level GUI frameworks and never see this layer. I don't mean doing stuff like MVC; that's IMO at the higher level. I'm talking about like I had to worry about here with tracking mouse clicks and that kind of crap. Other things like detecting dirty regions and reducing draw calls or whatever.

BlackMK4
Aug 23, 2006

wat.
Megamarm

Very cool, thank you :)

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.

GrumpyDoctor posted:

How do you embed that into the 2D plane though? I'm not speaking rhetorically, I've never heard of this.

This Stack Overflow answer does a good job of describing a good algorithm for this.

raminasi
Jan 25, 2005

a last drink with no ice

Sinestro posted:

This Stack Overflow answer does a good job of describing a good algorithm for this.

This (finding Voronoi diagrams that approximate arbitrary images) is neat as hell, but neither part of this

Sinestro posted:

Using the pixels of an image as the point set, and using color difference as the distance metric.

is involved in any of these solutions.

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
It's almost like I wrote the first one on my phone not really thinking about it and just saying something that sounded right-ish, but then get home and responded with actual research and thought. :ssh:

Sinestro fucked around with this message at 02:47 on Jul 25, 2015

Adbot
ADBOT LOVES YOU

dougdrums
Feb 25, 2005
CLIENT REQUESTED ELECTRONIC FUNDING RECEIPT (FUNDS NOW)
I'm making a website and I want to use websockets, but I can't really figure out how much they'll get blocked on the client side. Web searches just seem to indicate that it is a problem, but I can't figure out the extent of it. And if they are blocked, how might I account for that? The last time I made a website beyond simple text and headers, full duplex for the web didn't exist afaik, so these are new waters for me.

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