Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

09-18-2017 , 04:45 PM
Doing a project where I have, say, a mix of 500 IP address and IP ranges. I have a second, smaller set of IPs and ranges that need to be removed from the original list.

What would be the way to handle this?

I was thinking maybe use python and put both lists into files. Read each file into a variable/python list, where a single IP is a single entry, and a range is itself a list with the starting and ending IP as the two list items.

From there, iterate over the 2nd list and check if the items are equal or if the item in the 2nd list is within the range of the first. If so, I'm not entirely sure how to remove the item or alter it.

Like, if an included range is 10.0.0.1 through 10.0.0.10, and one to be excluded is 10.0.0.5, I need the first list to then contain [10.0.0.1, 10.0.0.4] and also [10.0.0.6, 10.0.0.10]

Does that make sense? How would I approach that second part? And is there a good python module that makes working with and comparing IPs easy? (Or ruby or bash)

Edit

Looks like python 3 has the ipaddress module which at least lets me easily compare addresses, so that's a start.

Last edited by Loki; 09-18-2017 at 04:59 PM.
Programming homework and newbie help thread Quote
09-18-2017 , 07:08 PM
Quote:
Originally Posted by Noodle Wazlib
Doing a project where I have, say, a mix of 500 IP address and IP ranges. I have a second, smaller set of IPs and ranges that need to be removed from the original list.

What would be the way to handle this?

I was thinking maybe use python and put both lists into files. Read each file into a variable/python list, where a single IP is a single entry, and a range is itself a list with the starting and ending IP as the two list items.

From there, iterate over the 2nd list and check if the items are equal or if the item in the 2nd list is within the range of the first. If so, I'm not entirely sure how to remove the item or alter it.

Like, if an included range is 10.0.0.1 through 10.0.0.10, and one to be excluded is 10.0.0.5, I need the first list to then contain [10.0.0.1, 10.0.0.4] and also [10.0.0.6, 10.0.0.10]

Does that make sense? How would I approach that second part? And is there a good python module that makes working with and comparing IPs easy? (Or ruby or bash)

Edit

Looks like python 3 has the ipaddress module which at least lets me easily compare addresses, so that's a start.
I don't know how fast it is but array slicing can maybe help with splitting lists:

http://pythoncentral.io/how-to-slice...les-in-python/

Also search for "most efficietnt algsorithm for finding an integer in a range". It help you with determining if ips belong in a range.

From what I remember it goes something like this:

1. Create and sort separate arrays for your starting points and ending points
2. Use a merge sort type of method to walk through the 2 arrays and determine if some value is still active in some main list of objects.

It was used to solve a separate problem (like counting integers still active in a larger range) but it might help with you determining if a value is within a set of ips and you can use the logic to potentially do what you need.

Also I feel like that since ips can be expressed easily as binary numbers there could be some binary manipulation trickery that I can't think of at the moment that would do the job but I may be over reaching there haha.

Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
09-18-2017 , 07:53 PM
Appreciate the tips. I think I got as far as I can, which is to say mostly done. Or at least as far as I'm willing to spend time working on this. There's some odd bugs and things I have to use other scripts to tidy up beforehand, but it cuts my work load drastically as is.

There was supposed to be a range function in one of the modules but I never could get it to work. The ipaddress mod really helped since I could compare greater or less than for actual IP addresses in IP form without wonky conversions.
Programming homework and newbie help thread Quote
09-18-2017 , 08:24 PM
Quote:
Originally Posted by Noodle Wazlib
Appreciate the tips. I think I got as far as I can, which is to say mostly done. Or at least as far as I'm willing to spend time working on this. There's some odd bugs and things I have to use other scripts to tidy up beforehand, but it cuts my work load drastically as is.

There was supposed to be a range function in one of the modules but I never could get it to work. The ipaddress mod really helped since I could compare greater or less than for actual IP addresses in IP form without wonky conversions.
I forgot to ask if subnetting will be an issue for your application but by the sounds of it probably not.

Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
09-19-2017 , 03:43 AM
Postgresql has an IP address datatype, maybe enough to get the job done on it's own.
Programming homework and newbie help thread Quote
09-19-2017 , 01:10 PM
How about the brute force method?

Convert both lists into pow(2, 32) sized bit-arrays, A and B, where each index represents the value of the ip address converted to an integer and the bit represents the existence of that ip address in the respective list. Then run:

Code:
A = map(A, (b, i) => { B[i] ? 0 : A[i] })
Takes a "bit" of memory About half gig per array.
Programming homework and newbie help thread Quote
09-20-2017 , 12:40 AM
Quote:
Originally Posted by RustyBrooks
(btw have you tried using a debugger?)
No, but I really should. I definitely need to learn GDB and Valgrind instead of relying on print statements. I never figured out what the problem was. I rewrote the loop and it fixed the seg. fault even though it shouldn't have: http://dpaste.com/0J0BEKN
Programming homework and newbie help thread Quote
09-21-2017 , 06:40 PM
Quote:
Originally Posted by Jeff W
No, but I really should. I definitely need to learn GDB and Valgrind instead of relying on print statements. I never figured out what the problem was. I rewrote the loop and it fixed the seg. fault even though it shouldn't have: http://dpaste.com/0J0BEKN
It works because you never have an input larger than buffsize and/or you are allocating an array from memory that is probably initialized to zero (haven't seen the whole program). Try moving buffer inside the while loop and see what happens especially with an input larger than buffsize. Pretty sure what I suggested in a previous post would have worked.
Programming homework and newbie help thread Quote
09-22-2017 , 02:34 AM
Quote:
Originally Posted by adios
It works because you never have an input larger than buffsize and/or you are allocating an array from memory that is probably initialized to zero (haven't seen the whole program). Try moving buffer inside the while loop and see what happens especially with an input larger than buffsize. Pretty sure what I suggested in a previous post would have worked.
I tried your solution previously and I still got a seg. fault. I think the problem may have been that:
Code:
char* buffer = new char[buffsize];
was not initializing the char[] to have a null terminator and
Code:
char buffer [buffsize];
implicitly was(automatic vs static array allocation?), but I'm not sure. In that case I would need to apply your solution, but also manually initialize buffer with a null terminator:

Code:
char* buffer = new char[buffsize + 1];
buffer[buffsize + 1] = '\0';
or it would seg. fault when a blank line with no null terminator is passed to strlen or strcpy.
Programming homework and newbie help thread Quote
09-22-2017 , 07:46 AM
Whether you need to add nulls of your own depends on what cin does. If cin reads "foo" from stdin, does it store
f o o NULL
or just
f o o

I suspect it stores it with the null. Which means it would work all the time, EXCEPT when your buffer was 3 characters long, long enough for f o o but not enough for the null, so you'd be putting the null into some other memory space.

I should probably just try it but I've been lazy. If cin doesn't include the terminating null then your solution won't work - you'd need to fill the whole buffer with zeros, because the string you read could be shorter than bufsize. You wouldn't get a segfault, because you WOULD have a terminator within your buffer, but you could have garbage characters in between.
Programming homework and newbie help thread Quote
10-13-2017 , 06:02 PM
Hi all, im trying to write a program for HUD purposes, and i need to trick HM2 into believing my windows form is a party poker table. I really did alot of research and found nothing usefull. What traits i need to extract from party poker table and give them to my windows form that HM2 will recognise it as PP table?
Programming homework and newbie help thread Quote
10-13-2017 , 06:14 PM
What, like you want HM2 to overlay it's HUD onto your window or something? I'm not really clear what you're trying to do
Programming homework and newbie help thread Quote
10-13-2017 , 06:58 PM
Yes, exactly that. I want HM to detect my windows form as a party table, and display a HUD over it. Im trying to find a way to make HM2 believe that my window is a party poker table.
Programming homework and newbie help thread Quote
10-13-2017 , 08:12 PM
Hm. Well, HM has a lot of options on how to identify the tables. I wrote a HUD myself many years ago and generally I would look for window titles that looked right. I don't remember the details but I think I looked 2 levels deep. That is, windows are created by a "parent" window so I'd look for, say, a window that had the right format for a poker table, who's parent had the right format for the poker client.

In some cases possibly I looked at the executable name that was launching the windows, I don't remember.

There may be other distinguishing marks they look for, it's a little hard to guess. There are techniques for debugging this kind of thing but they are not straightforward unless you have some experience with the windows API.
Programming homework and newbie help thread Quote
10-14-2017 , 04:18 AM
At the least - replicate process name, window title and window class (should actually be pretty easy for Party tables). HM1 had a "target window finder" thing like a crosshair you could tell it what window to display on - I guess this is no longer a feature in HM2?
Programming homework and newbie help thread Quote
10-28-2017 , 12:24 AM
I have an insidious bug in my C++ project involving std::getline.

When I call foo.x < infile, the loops with getline works until it hits line 27 of the infile:

Code:
8flatbed  Driver_8_ID Driver_8_CDL 8 50
At which point it starts an infinite loop of writing null to buffer. A similar problem with getline() grabbing empty lines unexpectedly occurs with other input files. I cannot discern any pattern or reason for this.

I have posted a truncated version of my main() function here: http://dpaste.com/1FB9C3R

and an example infile here: http://dpaste.com/18F7J23

Anyone see what I'm doing wrong?
Programming homework and newbie help thread Quote
10-28-2017 , 11:31 AM
Well, to begin, I don't think getline() returns a boolean value. It returns a reference to the stream object. So your program is never going to exit. If it fails to read a line (doesn't find the delimiter/gets to EOF) then it'll set a failbit and/or and eofbit on the stream object, which you can use as a loop exit condition (failbit and eofbit are a bit different but using either one should work for your case)
Programming homework and newbie help thread Quote
10-28-2017 , 11:49 AM
Quote:
Originally Posted by RustyBrooks
Well, to begin, I don't think getline() returns a boolean value. It returns a reference to the stream object.
Are you referring to while(std::cin.getline(buffer, maxChar)) ? This works but I'm not sure of the implementation details--the loop terminates when EOF is reached.

Anyway, the problem was that my buffer wasn't allocated enough space. Thanks.
Programming homework and newbie help thread Quote
10-28-2017 , 12:19 PM
Interesting. Maybe the bool operator for istream just returns the fail flag.

ETA: yeah
http://www.cplusplus.com/reference/i...operator_bool/
Programming homework and newbie help thread Quote
10-28-2017 , 08:56 PM
From the getline() documentation re: the streamsize parameter:

Quote:
Maximum number of characters to write to s (including the terminating null character).
If the function stops reading because this limit is reached without finding the delimiting character, the failbit internal flag is set.
streamsize is a signed integral type.
Your call to getline() should be:

std::cin.getline(buffer, maxChar + 1)
Programming homework and newbie help thread Quote
10-29-2017 , 02:29 AM
Quote:
Originally Posted by toddw8
From the getline() documentation re: the streamsize parameter:

Your call to getline() should be:

std::cin.getline(buffer, maxChar + 1)
Thanks for the info. I had assumed that cin.getline() would overwrite the null terminator when the instream length >= buffer length.
Programming homework and newbie help thread Quote
11-04-2017 , 04:26 PM
If anyone is good with matplotlib in Python I could use some help for this physics lab I'm doing. I have a large csv file that I plotted as a histogram. I'm asked to find the x-value of the peak of the histogram and I can't figure out how to do that. Not sure if you need my code but here it is.
Edit: It asks me to do this before fitting a Guassian to the curve.

Code:
def dataPlot(file, title) :
    data = np.loadtxt(file, delimiter=',',skiprows=7)
    xdata = data[:,0]
    ydata = data[:,1]
    data = []
    for i in range(len(xdata)) : #Parse csv into one list of the data
        for j in range(int(ydata[i])) :
            data.append(xdata[i])
    data = np.array(data)
    voltage = data*0.000122 #Convert channels to voltage mV
    energy = (voltage/(G1*G2)/1.6e-13) #convert voltage to energy MeV

    plt.hist(energy,bins=500, edgecolor="blue")
    plt.xlabel("Alpha Energy [MeV]")
    plt.ylabel('Count')
    plt.title("Alpha Energies Detected for %s"%(title))
    plt.show()

Last edited by matrat; 11-04-2017 at 04:33 PM.
Programming homework and newbie help thread Quote
11-04-2017 , 04:54 PM
Ok nevermind I finally figured it out.

Code:
    n, bins, patches = plt.hist(energy,bins=500, edgecolor="blue")
    plt.xlabel("Alpha Energy [MeV]")
    plt.ylabel('Count')
    plt.title("Alpha Energies Detected for %s"%(title))
    plt.show()
    
    maxCount = np.argmax(n)
    print(bins[maxCount])
Programming homework and newbie help thread Quote
11-14-2017 , 09:33 PM
I'm making a basic forum type app and want to get the 5 most recently commented on threads in order. My database has a threads table and a comments table with a threadID. Would it be bad to update some column on the thread table for each new comment? Like a lastUpdated field? Or should I figure out how to get an sql query to return threads ordered by their most recent comment via a join somehow?
Programming homework and newbie help thread Quote
11-14-2017 , 09:43 PM
Group comments table by threadid, selecting threadid and MAX(commentdate), order results by MAX(commentdate) descending, choose top 5. Use resulting threadids to show threads.
Programming homework and newbie help thread Quote

      
m