Author Topic: [Python] Simple suite totalizer  (Read 9352 times)

0 Members and 1 Guest are viewing this topic.

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
[Python] Simple suite totalizer
« on: August 17, 2012, 01:17:16 PM »
+1
This is a very fast and simple yet useful script that will sum up all the caracteristics of the items you are currently wearing in less that 60 rows of code. :)
Intended to be used with Python 3.x

Code: [Select]
""" This small script will calculate the summ of the suite your char is currentrly wearing.
This will only run in Python 3.x"""

import stealth
import re

uo_layers = (ArmsLayer(), BeardLayer(), BraceLayer(), CloakLayer(), EarLayer(), EggsLayer(), GlovesLayer(), HairLayer(), HatLayer(), LegsLayer(),
LhandLayer(), NeckLayer(), PantsLayer(), RhandLayer(), RingLayer(), RobeLayer(), ShirtLayer(), ShoesLayer(), TalismanLayer(), TorsoHLayer(),
TorsoLayer(), WaistLayer())

properties = {}

def to_sum_up(prop : 'string') -> 'Boolean':
""" This sub will be used to get rid of unwanted properties like 'Insured' or such.
As argument it is expecting a string and it will retur either True (if the property should not be ignored) or False"""

# strigs to be ignored in tooltip. Add new if missing
ignores = ['Insured', 'durability', 'Weight', 'artifact rarity', 'strength requirement', 'two-handed weapon', 'skill required']

for ignore in ignores:
if prop.find(ignore) != -1 or prop == '':
break
else:
return True

return False

def sum_props(dressed_layers : 'dict') -> 'dict':
""" This sub will sum all the properties together.
As argument it is expeting a dictionary in this format {layer:[raw properties,]}
It is returning another dictionary with this format {prop name : total value}"""

totals = {}

# Property regex
reProps = re.compile(r"([a-zA-Z ].+?)[+]{0,}([\d]{1,})(%{0,1})")
for layer in dressed_layers:
if dressed_layers[layer]:
item = dressed_layers[layer][0]
props = dressed_layers[layer][1:]
for prop in props:
match = reProps.search(prop)
if match:
prop_name = match.group(1).strip()
prop_value = match.group(2)
totals[prop_name] = totals.get(prop_name, 0) + int(prop_value)

return totals

for uo_layer in uo_layers:
weared = ObjAtLayer(uo_layer)
if weared > 0:
properties[hex(uo_layer)] = [] + [x.strip() for x in GetTooltip(weared).split(' | ') if to_sum_up(x)]

totals = sum_props(properties)

for prop in sorted(totals.keys(), key=str.lower):
print(prop + ' --> ' + str(totals[prop]))
Member of the Stealth development team.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: [Python] Simple suite totalizer
« Reply #1 on: August 17, 2012, 02:09:31 PM »
+1
Ooooh, nice.  (boy Python makes my head hurt.....)  heh
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
Re: [Python] Simple suite totalizer
« Reply #2 on: August 17, 2012, 02:33:01 PM »
+1
Yes, i understand the feeling; my first times at Python were like: "WTF!"  :P
Then I hacked with it here and there and eventually read Learning Python and now I believe that all scripting interfaces should be made in Python. ;)

How many lines would it take to make a suite totalizer in EUO? This is why Python is cool in my opinion...
« Last Edit: August 17, 2012, 02:40:00 PM by Boydon »
Member of the Stealth development team.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: [Python] Simple suite totalizer
« Reply #3 on: August 17, 2012, 03:14:23 PM »
+1
Quote from: Boydon
How many lines would it take to make a suite totalizer in EUO? This is why Python is cool in my opinion...

http://www.scriptuo.com/index.php?topic=15.msg15#msg15

Exactly 993 in EUO.  ;)

And I highly recommend that book too.  It's how I learned as well.  I'll try and find the other couple references I used since they were also pretty good.
« Last Edit: August 17, 2012, 03:17:46 PM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: [Python] Simple suite totalizer
« Reply #4 on: August 18, 2012, 10:14:24 AM »
+1
Turns out I have a bunch of Python books, but the one I seem to always come back to for reference is:

Python in a Nutshell

Others include:
Programming Python
and
Dive into Python
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
Re: [Python] Simple suite totalizer
« Reply #5 on: August 18, 2012, 01:23:58 PM »
+1
Your script also has a menu, mine just prints out to the console. Yet I belive that with tkinter it would take much more less than 993 lines. ;)

Programming Python is the one I'm currently on. :)
Member of the Stealth development team.

Offline Crome969

  • Moderator
  • *
  • *****
  • Posts: 2098
  • Activity:
    0%
  • Reputation Power: 25
  • Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.
  • Gender: Male
  • UO Enthusiast
  • Respect: +211
  • Referrals: 10
    • View Profile
    • ScriptSDK
Re: [Python] Simple suite totalizer
« Reply #6 on: August 23, 2012, 04:26:28 AM »
+1
Good Job boydon,
even when i hate Python :)

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: [Python] Simple suite totalizer
« Reply #7 on: August 23, 2012, 09:17:54 AM »
+1
Hey Boydon, how far have you taken the GUI investigation within Python?  Are you experimenting with that at all?
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline BoydonTopic starter

  • Moderator
  • **
  • *****
  • Posts: 76
  • Activity:
    0%
  • Reputation Power: 3
  • Boydon has no influence.
  • Respect: +16
  • Referrals: 0
    • View Profile
Re: [Python] Simple suite totalizer
« Reply #8 on: August 24, 2012, 07:08:15 AM »
+1
I didn't go so far in the end. I hacked a bit with both Wx and PyQt, but I'm uncertain 'bout what to do.

Wx would be my favorite choice cause i prefer it over PyQt, but the problem is that right now it is only available for Python 2.x. and I strongly believe that 3.x is the future. Probably both of them are too much overhead and the standard tkinter is all you need for simple graphic interfaces (think back to what you are used in EUO... Tkinter has all of it and more!).

My main problem is that I'm not very well confident with threads in Python and if you want GUIs you need them! GUI should use the main thread and script logic should run in a separate one in most of the cases. :)
Member of the Stealth development team.

Offline Crome969

  • Moderator
  • *
  • *****
  • Posts: 2098
  • Activity:
    0%
  • Reputation Power: 25
  • Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.
  • Gender: Male
  • UO Enthusiast
  • Respect: +211
  • Referrals: 10
    • View Profile
    • ScriptSDK
Re: [Python] Simple suite totalizer
« Reply #9 on: August 24, 2012, 10:54:05 PM »
+1

Tags: Stealth  python