xgeovanni

utility.py

Aug 17th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. #Random useful functions
  2. #Bobby Clarke
  3. #Open Source License
  4.  
  5. class misc():
  6.     def spaceTuple(totuple):
  7.         """Takes a string and creates a tuple from it, elements are seperated by spaces"""
  8.  
  9.         newtuple = []
  10.         newelement = ""
  11.  
  12.         for char in str(totuple):
  13.             if char != " ":
  14.                 newelement += char
  15.                 print(newelement)
  16.             else:
  17.                 newtuple.append(newelement)
  18.                 newelement = ""
  19.  
  20.         if newelement:
  21.             newtuple.append(newelement)
  22.  
  23.         return tuple(newtuple)
  24.  
  25.     def get_dir(files = False):
  26.         """Get the directory the program is in, have True as a parameter to get a list of the files within said directory"""
  27.  
  28.         import sys
  29.         import os
  30.        
  31.         selfdir = os.path.dirname(sys.argv[0])
  32.  
  33.         if files:
  34.             return os.listdir(selfdir)
  35.         else:
  36.             return selfdir
  37.  
  38. class maths():
  39.     import math
  40.  
  41.     def isWholeNumber(num):
  42.         if num == round(num):
  43.             return True
  44.         else:
  45.             return False
  46.  
  47.     def median(nums):
  48.         """Take a list of numbers and return the median"""
  49.         import math
  50.  
  51.         halflen = len(nums) / 2
  52.         nums.sort()
  53.  
  54.         if isWholeNumber(halflen):
  55.             return nums[halflen]
  56.         else:
  57.             return nums[math.floor(halflen)] + nums[math.ceil(halflen)] / 2
  58.  
  59.     def adv_range(limit1, limit2 = None, increment = 1.):
  60.         """Range function that accepts floats (and integers)."""
  61.  
  62.         if limit2 is None:
  63.             limit2, limit1 = limit1, 0.
  64.         else:
  65.             limit1 = float(limit1)
  66.  
  67.         count = int(math.ceil(limit2 - limit1)/increment)
  68.         return (limit1 + n*increment for n in range(count))
  69.  
  70.     def chance(num, outof):
  71.         """returns a bool, the chance of it being true = num / outof"""
  72.         import random
  73.  
  74.         if random.randint(1, outof) in range(num):
  75.             return True
  76.         else:
  77.             return False
  78.  
  79. class pygame():
  80.     try:
  81.         import pygame
  82.     except ImportError:
  83.         raise ImportError("You do not appear to have pygame, get it at www.pygame.org")
  84.  
  85.     def imageSizeRatio(surface1, surface2):
  86.         """Finds the width and height ratio between two pygame.Surface objects and returns them,
  87.         the screen can also be used for use in resizable or fullscreen applications"""
  88.         if not isinstance(surface1, pygame.Surface) or not isinstance(surface2, pygame.Surface):
  89.            raise TypeError("imageSizeRatio requires two pygame.Surface objects")
  90.  
  91.         scale1 = (surface1.get_width(), surface1.get_height())
  92.         scale2 = (surface2.get_width(), surface2.get_height())
  93.  
  94.         #Index 0 represents width and index 1 represents height
  95.  
  96.         if scale1[0] > scale2[0]:
  97.             widthRatio =  scale1[0] / scale2[0]
  98.         else:
  99.             widthRatio =  scale2[0] / scale1[0] #also catches if equal, thus returning 1
  100.  
  101.         if scale1[1] > scale2[1]:
  102.             heightRatio  = scale1[1] / scale2[1]
  103.         else:
  104.             heightRatio  = scale2[1] / scale1[1]
  105.  
  106.         return widthRatio, heightRatio
  107.  
  108.     def areaclick(area, mouse = pygame.mouse.get_pos()):
  109.         """mouse must be a tuple or list with 2 elements describing the x and y co - ordinates of the mouse, use pygame.mouse.get_pos(),
  110.        area must be a tuple or list of 4 numbers creating a rectangle like so: (top left, bottom left, top right, bottom right), returns a bool"""
  111.  
  112.         if mouse[0] in range(area[0], area[1]) and mouse[1] in range(area[2], area[3]):
  113.             return True
  114.         else:
  115.             return False
  116.  
  117. if __name__ == "__main__":
  118.     print("This is a group of useful open source functions crated by Bobby Clarke")
  119.     input()
Advertisement
Add Comment
Please, Sign In to add comment