Python Forum
mutable values to string items?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mutable values to string items?
#12
Context helps when the question doesn't make any sense (my issue, not yours). I'm still a little fuzzy about this entire thread. But if you do want to save information in a file, and you like Gribouillis' post, I think you will like json even better.

An advantage of json over csv is that there are no intermediate steps required to translate the Python object you want to save to something that can be written to a file. The json dump command does this for you. The same goes in the reverse direction using json load. The result of reading (loading) a json file is a Python object that you can use without any further processing.

Here I translate Gribouillis' csv code to similar code that uses json. I changed the channels datatype from list to dictionary, but json can also read/write a list of lists.
import json
from pathlib import Path
 
source = "plugins.var.python.limiter.limiter_chans.ircnet #python,#forum"
 
# extract the list ['#python', '#forum']
ichan = [x.strip() for x in source.split(' ', 1)[1].strip().split(',')]

# create the dictionary
channels = {x:0 for x in ichan}

# optional: save content to file
filename = Path(__file__).parent/'channels.json'
with open(filename, "w") as file:
    json.dump(channels, file)
 
# optional: read file to get content
with open(filename, "r") as file:
    channels = json.load(channels, file)
 
print(channels) # prints {'#python':0, '#forum':0}
The disadvantage of json is that a csv file is prettier when you print it out. Json files are human readable but look more like html than a table.

I'm still not seeing the reason for a file unless you need some kind of persistence.

I did take a shot at modifying your "new" code to be more like the old. Instead of a static network and list of channels, that information is loaded from configuration information the first time limiter_cb runs. From then on it looks exactly like your old code.
# Simple Channel limiter version 0.1.
# Protection script against mass joining from clones/botnets on your channel.
  
''' /set plugins.var.python.limiter.limiter_chans.ircnet #python,#forum '''
  
from weechat import (
    WEECHAT_RC_OK, register, infolist_get, infolist_string, buffer_search,
    config_get_plugin, infolist_next, string_eval_expression, command, hook_timer, infolist_free)
  
NAME = 'limiter'
AUTHOR = 'fozz <[email protected]>'
VERSION = '0.1'
LICENSE = 'GPL3'
DESCRIPTION = 'Channel limiter, protection against mass joining from clones/botnets'
  
register(NAME, AUTHOR, VERSION, LICENSE, DESCRIPTION, '', '')
  
# Configuration 
limit_ = 5
min_offset_ = 2 #lowest diff
max_offset_ = 2 #highest diff
interval_ = 1
channels_ = []

class Channel:
    def __init__(self, network, channel, offset=0):
        self.network = network
        self.channel = channel
        self.offset = offset

def initialize_channel_list():
    networks = infolist_get('irc_server', '', '')
    while infolist_next(networks):
        network = infolist_string(networks, 'name')
        limiter_chans = config_get_plugin('limiter_chans.{}'.format(network))
        if limiter_chans:
            for channel in limiter_chans.split(','):
                channels_.append(Channel(network, channel))
    infolist_free(networks)

def limiter_cb(data, signal):
    if len(channels_) == 0:
        initialize_channel_list()

    for channel in channels_:
        buffer = buffer_search('irc', '{}.{}'.format(channel.network, channel.channel))
        if buffer:
            nc = int(string_eval_expression('${buffer.nicklist_visible_count}', {'buffer': buffer}, {}, {}))
            nl = nc + limit_ 
            minoff = nl - min_offset_
            maxoff = nl + max_offset_
            if channel.offset <= minoff or channel.offset >= maxoff:
                command(buffer, '/mode ' + channel + ' +l ' + f'{nl}')
                channel.offset = nl  # save value for next time

    return WEECHAT_RC_OK
  
hook_timer(interval_ * 60000, 0, 0, 'limiter_cb', '')
fozz likes this post
Reply


Messages In This Thread
mutable values to string items? - by fozz - Aug-21-2022, 08:06 PM
RE: mutable values to string items? - by snippsat - Aug-21-2022, 08:58 PM
RE: mutable values to string items? - by rob101 - Aug-21-2022, 09:02 PM
RE: mutable values to string items? - by deanhystad - Aug-22-2022, 02:43 AM
RE: mutable values to string items? - by DeaD_EyE - Aug-22-2022, 07:58 AM
RE: mutable values to string items? - by fozz - Aug-22-2022, 04:18 PM
RE: mutable values to string items? - by fozz - Aug-29-2022, 02:24 PM
RE: mutable values to string items? - by deanhystad - Aug-29-2022, 07:33 PM
RE: mutable values to string items? - by fozz - Aug-29-2022, 09:01 PM
RE: mutable values to string items? - by fozz - Aug-29-2022, 08:43 PM
RE: mutable values to string items? - by deanhystad - Aug-30-2022, 05:25 AM
RE: mutable values to string items? - by deanhystad - Aug-30-2022, 03:20 PM
RE: mutable values to string items? - by fozz - Aug-30-2022, 06:37 PM
RE: mutable values to string items? - by deanhystad - Aug-30-2022, 07:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using mutable in function defintion as optional paramter akbarza 8 667 Apr-27-2024, 09:59 PM
Last Post: snippsat
  mutable argument in function definition akbarza 1 564 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Trying to compare string values in an if statement israelsattleen 1 619 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Getting rid of old string values Pedroski55 3 1,108 Oct-11-2022, 10:56 PM
Last Post: Pedroski55
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,525 Apr-11-2021, 11:03 PM
Last Post: lastyle
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 6,970 Dec-25-2020, 02:31 AM
Last Post: tinh
  Mutable Strings millpond 3 2,707 Aug-24-2020, 08:42 AM
Last Post: millpond
  Function parameters and values as string infobound 1 1,823 Jul-24-2020, 04:28 AM
Last Post: scidam
  xml.etree.ElementTree extract string values matthias100 2 5,164 Jul-12-2020, 06:02 PM
Last Post: snippsat
  What is the meaning of mutable data type? qliu 3 3,076 Apr-17-2020, 07:20 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020