"Fossies" - the Fresh Open Source Software Archive

Member "radialnet/util/misc.py" (29 Feb 2008, 2672 Bytes) of package /linux/privat/old/radialnet-0.44.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "misc.py" see the Fossies "Dox" file reference documentation.

    1 # vim: set fileencoding=utf-8 :
    2 
    3 # Copyright (C) 2007, 2008 Insecure.Com LLC.
    4 #
    5 # Author: João Paulo de Souza Medeiros <ignotus21@gmail.com>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
   10 # (at your option) any later version.
   11 #
   12 # This program is distributed in the hope that it will be useful,
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 # GNU General Public License for more details.
   16 #
   17 # You should have received a copy of the GNU General Public License
   18 # along with this program; if not, write to the Free Software
   19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   20 
   21 from core.Coordinate import CartesianCoordinate
   22 from gui.RadialNet import *
   23 from util.geometry import *
   24 import math
   25 
   26 
   27 
   28 def ipv4_compare(ip1, ip2):
   29     """
   30     """
   31     ip1 = [int(i) for i in ip1.split('.')]
   32     ip2 = [int(i) for i in ip2.split('.')]
   33 
   34     for i in range(4):
   35 
   36         if ip1[i] != ip2[i]:
   37 
   38             if ip1[i] < ip2[i]:
   39                 return -1
   40 
   41             else:
   42                 return 1
   43 
   44     return 0
   45 
   46 
   47 def list_difference_update(list, difference):
   48     """
   49     """
   50     for item in difference:
   51 
   52         if item in list:
   53             list.remove(item)
   54 
   55 
   56 def list_update(list, append):
   57     """
   58     """
   59     for item in append:
   60 
   61         if item not in list:
   62             list.append(item)
   63 
   64 
   65 def swap(list, a, b):
   66     """
   67     """
   68     list[a], list[b] = list[b], list[a]
   69 
   70 
   71 def sort_children(children, father):
   72     """
   73     """
   74     if len(children) < 2:
   75         return children
   76 
   77     # create angle reference
   78     f_x, f_y = father.get_cartesian_coordinate()
   79 
   80     for child in children:
   81 
   82         c_x, c_y = child.get_cartesian_coordinate()
   83         _, angle = CartesianCoordinate(c_x - f_x, c_y - f_y).to_polar()
   84 
   85         child.set_draw_info({'angle_from_father': math.degrees(angle)})
   86 
   87     return sort_children_by_angle(children)
   88 
   89 
   90 def sort_children_by_angle(children):
   91     """
   92     """
   93     if len(children) < 2:
   94         return children
   95 
   96     vector = list()
   97     vector.append(children.pop())
   98 
   99     for a in children:
  100 
  101         theta_a = normalize_angle(a.get_draw_info('angle_from_father'))
  102 
  103         for i in range(len(vector) -1, -1, -1):
  104 
  105             b = vector[i]
  106 
  107             theta_b = normalize_angle(b.get_draw_info('angle_from_father'))
  108 
  109             if theta_b <= theta_a <= theta_b + 180:
  110 
  111                 vector.insert(i + 1, a)
  112                 break
  113 
  114         else:
  115             vector.insert(0, a)
  116 
  117     return vector