RefCount library

Submit modules/packages for inclusion with Source.Python.
User avatar
BackRaw
Senior Member
Posts: 413
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany

RefCount library

Postby BackRaw » Sun Feb 08, 2015 8:16 pm

I thought sometimes you may need to use refcounts for variables, so I put something together here:

Syntax: Select all

# ../addons/source-python/packages/source-python/refcount.py


# =============================================================================
# >> IMPORTS
# =============================================================================

# maybe import and do some logging like in the other libs..


# Source.Python
# Cvars
from cvars import ConVar


# =============================================================================
# >> CLASSES
# =============================================================================

class _Variables(dict):

"""Class used to provide refcount functionality for server variables."""

def __init__(self):
"""Call dict's __init__() method."""
super(_Variables, self).__init__()

def __missing__(self, variable):
"""Adds the variable to this dict, if it doesn't exist yet."""
# Set the variable's count to 0
self[variable] = 0

def increment(self, variable, desired_value=1):
"""Increments the variable's refcount and sets
the variable's value to the desired one, if it isn't already set."""
# get a ConVar instance of the variable
cvar = ConVar(variable)

# increment the refcount for the variable
self[variable] += 1

# set it's value to the desired one, if it isn't already set
if cvar.get_int() != desired_value:
cvar.set_int(desired_value)

# TODO: add support for other variable types

def decrement(self, variable):
"""Decrements the variable's refcount and sets
the variable's value to its default, if it isn't already set."""
# get a ConVar instance of the variable
cvar = ConVar(variable)

# decrement the refcount for the variable
self[variable] -= 1

# is it time to set the variable's value back to default?
if self[variable] <= 0:

# if yes, set the variable's value back to default
# if it isn't already set
if cvar.get_int():
cvar.revert()

# remove the variable from this dict as it is not needed anymore
del self[variable]


# Instantiate the _Variables class
variables = _Variables()
Do we even need something like this? lol :D
My Github repositories:

Source.Python: https://github.com/backraw/backraw.sp
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Feb 16, 2015 3:27 pm

There has been some discussion of this internally, and it wouldn't necessarily be a bad thing to include. Though, I think I would like this implementation a little better:

Syntax: Select all

from collections import defaultdict

from cvars import ConVar

class _RefCount(defaultdict):

def __setitem__(self, item, value):
convar = ConVar(item)
current_value = convar.get_int()
if value > 0 and current_value != 1:
convar.set_int(1)
elif value == 0 and current_value != 0:
convar.set_int(0)
elif value < 0:
raise ValueError('Reference count cannot be a negative number.')
super(_RefCount, self).__setitem__(item, value)

reference_count = _RefCount(int)


And usage would be:

Syntax: Select all

from refcount import reference_count

reference_count['my_variable'] += 1

def unload():
reference_count['my_variable'] -= 1


Though, I am not sure what all the counter would be used for, so there might need to be more to it than that. Perhaps we could add a flag attribute to add/remove specified flags, as well as support different start and end values. That last part could get tricky, as scripts could possibly want to have different values setup for that.
Image
User avatar
Ayuto
Project Leader
Posts: 1619
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Feb 16, 2015 9:10 pm

Hmm, I don't think we need something like that. Back in ES I have never seen anyone using that except for eventscripts_noisy and maybe sv_cheats. If you should ever need to do something like that, it's not hard to do this manually.

Syntax: Select all

sv_cheats = ConVar('sv_cheats')

def load():
sv_cheats.set_int(sv_cheats.get_int() + 1)

def unload():
sv_cheats.set_int(sv_cheats.get_int() - 1)

Return to “Module/Package Submissions”

Who is online

Users browsing this forum: No registered users and 1 guest